• Home
  • ES6
  • ADVANCED
    • Javascript Array Methods
    • Javascript String Methods
    • Javascript Regex
    • ES Next
  • JS BOM
  • JS DOM
  • WEB API
  • SNIPPETS
  • TYPESCRIPT
  • Golang
  • Dark Mode Light Mode
  • » Javascript Fundamental. » javaScript-variables
    <h1 id="javascript-variables">JavaScript Variables</h1> <p><strong>Summary</strong>: in this tutorial, you’ll learn about JavaScript variables and how to use variables to store values in the application.</p> <p>A variable is a label that references a value like a number or string. Before using a variable, you need to declare it.</p> <p><a href="https://www.javascripttutorial.net/wp-content/uploads/2022/01/JavaScript-variables.svg"><img src="https://www.javascripttutorial.net/wp-content/uploads/2022/01/JavaScript-variables.svg" alt="img"></a></p> <h2 id="declare-a-variable">Declare a variable</h2> <p>To declare a variable, you use the <code>var</code> keyword followed by the variable name as follows:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">var</span> message;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>A variable name can be any valid identifier. By default, the <code>message</code> variable has a special value <a href="https://www.javascripttutorial.net/javascript-data-types/#undefined"><code>undefined</code></a> if you have not assigned a value to it.</p> <p>Variable names follow these rules:</p> <ul> <li>Variable names are case-sensitive. This means that the <code>message</code> and <code>Message</code> are different variables.</li> <li>Variable names can only contain letters, numbers, underscores, or dollar signs and cannot contain spaces. Also, variable names must begin with a letter, an underscore (<code>_</code>) or a dollar sign (<code>$)</code>.</li> <li>Variable names cannot use the reserved words.</li> </ul> <p>By convention, variable names use camelcase like <code>message</code>, <code>yourAge</code>, and <code>myName</code>.</p> <p>JavaScript is a dynamically typed language. This means that you don’t need to specify the variable’s <a href="https://www.javascripttutorial.net/javascript-data-types/">type</a> in the declaration like other static typed languages such as Java or <a href="https://www.csharptutorial.net/csharp-tutorial/csharp-variables/">C#</a>.</p> <p>Starting in ES6, you can use the <code>let</code> keyword to declare a variable like this:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>It’s a good practice to use the <code>let</code> keyword to declare a variable. Later, you’ll learn the differences <a href="https://www.javascripttutorial.net/es6/difference-between-var-and-let/">between <code>var</code> and <code>let</code> keywords</a>. And you should not worry about it for now.</p> <h2 id="initialize-a-variable">Initialize a variable</h2> <p>Once you have declared a variable, you can initialize it with a value. To initialize a variable, you specify the variable name, followed by an equals sign (<code>=</code>) and a value.</p> <p>For example, The following declares the <code>message</code> variable and initializes it with a literal string <code>&quot;Hello&quot;</code>:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message; message = <span class="hljs-string">&quot;Hello&quot;</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>To declare and initialize a variable at the same time, you use the following syntax:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> variableName = value;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>For example, the following statement declares the <code>message</code> variable and initializes it with the literal string <code>&quot;Hello&quot;</code>:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">&quot;Hello&quot;</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>JavaScript allows you to declare two or more variables using a single statement. To separate two variable declarations, you use a comma (<code>,</code>) like this:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">&quot;Hello&quot;</span>, counter = <span class="hljs-number">100</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>Since JavaScript is a dynamically typed language, you can assign a value of a different type to a variable. Although, it is not recommended. For example:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">&#x27;Hello&#x27;</span>; message = <span class="hljs-number">100</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <h2 id="change-a-variable">Change a variable</h2> <p>Once you initialize a variable, you can change its value by assigning a different value. For example:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">&quot;Hello&quot;</span>; message = <span class="hljs-string">&#x27;Bye&#x27;</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <h2 id="undefined-vs-undeclared-variables">Undefined vs. undeclared variables</h2> <p>It’s important to distinguish between undefined and undeclared variables.</p> <p>An undefined variable is a variable that has been declared but has not been initialized with a value. For example:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message); <span class="hljs-comment">// undefinedCode language: JavaScript (javascript)</span> </code></pre> <p>In this example, the <code>message</code> variable is declared but not initialized. Therefore, the <code>message</code> variable is undefined.</p> <p>In contrast, an undeclared variable is a variable that has not been declared. For example:</p> <pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(counter);<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>Output:</p> <pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(counter); ^ <span class="hljs-title class_">ReferenceError</span>: counter is not definedCode <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>In this example, the <code>counter</code> variable has not been declared. Hence, accessing it causes a <code>ReferenceError</code>.</p> <h2 id="constants">Constants</h2> <p>A constant holds a value that doesn’t change. To declare a constant, you use the const keyword. When defining a constant, you need to initialize it with a value. For example:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">const</span> workday = <span class="hljs-number">5</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>Once defining a constant, you cannot change its value.</p> <p>The following example attempts to change the value of the workday constant to 4 and causes an error:</p> <pre><code class="hljs language-js">workday = <span class="hljs-number">2</span>; </code></pre> <p>Error:</p> <pre><code class="hljs language-js"><span class="hljs-title class_">Uncaught</span> <span class="hljs-title class_">TypeError</span>: <span class="hljs-title class_">Assignment</span> to constant variable.<span class="hljs-property">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript) </code></pre> <p>Later, you’ll learn that the <code>const</code> keyword actually defines a read-only reference to a value in the <a href="https://www.javascripttutorial.net/es6/javascript-const/">constants</a> tutorial.</p> <h2 id="summary">Summary</h2> <ul> <li>A variable is a label that references a value.</li> <li>Use the <code>let</code> keyword to declare a variable.</li> <li>An undefined variable is a variable that has been declared but not initialized while an undeclared variable is variable that has not been declared.</li> <li>Use the <code>const</code> keyword to define a readonly reference to a value.</li> </ul>

    GETTING STARTED

  • What is JavaScript
  • Install a JavaScript Code Editor
  • Meet the Console Tab of Devtools
  • JavaScript Hello World
  • JAVASCRIPT FUNDAMENTALS

  • Syntax
  • Variables
  • Data Types
  • Number
  • Numeric Separator
  • Octal and Binary Literals
  • Boolean
  • String
  • Object
  • Primitive vs. Reference Values
  • Array
  • JAVASCRIPT OPERATORS

  • Arithmetic Operators
  • Remainder Operator
  • Assignment Operators
  • Unary Operators
  • Comparison Operators
  • Logical Operators
  • Logical Assignment Operators
  • Nullish Coalescing Operator
  • Exponentiation Operator
  • CONTROL FLOW

  • if
  • if else
  • if else if
  • Ternary Operator (:?)
  • switch case
  • while
  • do while
  • for
  • break
  • continue
  • Comma Operator
  • JAVASCRIPT FUNCTIONS

  • Functions
  • Functions are First-Class Citizens
  • Anonymous Functions
  • Understanding Pass-By-Value in JavaScript
  • Recursive Functions
  • Default Parameters
  • JAVASCRIPT OBJECTS

  • Object Methods
  • Constructor Functions
  • Prototype
  • Constructor/Prototype Pattern
  • Prototypal Inheritance
  • this
  • globalThis
  • Object Properties
  • for…in Loop
  • Enumerable Properties
  • Own Properties
  • Object.values()
  • Object.entries()
  • Object.is()
  • Factory Functions
  • Object Destructuring
  • Optional Chaining Operator
  • Object Literal Syntax Extensions
  • CLASSES

  • Class
  • Getters & Setters
  • Class Expressions
  • Computed Properties
  • Inheritance
  • new.target
  • Static Methods
  • Static Properties
  • Private Fields
  • Private Methods
  • instanceof Operator
  • ADVANCED FUNCTIONS

  • Function Type
  • The call() Method
  • The apply() method
  • The bind() Method
  • Closures
  • Immediately Invoked Function Expression (IIFE)
  • Returning Multiple Values
  • Arrow Functions
  • Arrow Functions: Do & Don’t
  • Rest Parameters
  • Callback
  • PROMISES & ASYNC/AWAIT

  • Promises
  • Promise Chaining
  • Promise.all()
  • Promise.race()
  • Promise.any()
  • Promise.allSettled()
  • Promise.prototype.finally()
  • Promise Error Handling
  • async/await
  • JAVASCRIPT RUNTIME

  • Execution Context
  • Call Stack
  • Event Loop
  • Hoisting
  • Variable Scopes
  • About Me

    • Made by Rasel Mahmud
    • Portfolio

    Top References

    • javascripttutorial.net
    • W3school.com

    Top Tutorials

    • Primitive vs. Reference Values
    • What is JavaScript

    NOTE:

    This website only for Developing purpose not for business. and all content copied from other place like javascripttutorial.net and W3school.com