<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>"Hello"</code>:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message;
message = <span class="hljs-string">"Hello"</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>"Hello"</code>:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">"Hello"</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">"Hello"</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">'Hello'</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">"Hello"</span>;
message = <span class="hljs-string">'Bye'</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>