JavaScript Tutorial

Javascript Variables

JavaScript variables are used to store and manipulate data within a program. They are declared using the keywords "var," "let," or "const" followed by the variable name. Variables in JavaScript are dynamically typed, meaning they can hold values of any data type.

Rules for declaring variables

Following are the rules used to declare any variable in javascript:-

  • Variable names must start with a letter (a-z, A-Z), underscore (_), or dollar sign ($).
  • Subsequent characters in the variable name can be letters, digits (0-9), underscores, or dollar signs.
  • JavaScript variables are case-sensitive, so "x" and "X" are distinct variables.
  • The "var" keyword has been traditionally used to declare variables, but "let" and "const" are preferred in modern JavaScript.
  • "let" and "const" introduce block-scoped variables, while "var" has function-level scope.
  • "let" allows variable reassignment, whereas "const" creates a read-only (immutable) variable.
  • It is recommended to use "let" for variables that may change and "const" for variables that remain constant.

Declaring a variable

When declaring variables in JavaScript, you need to follow certain rules and use the appropriate keywords. Here's an overview of declaring variables in JavaScript:

  1. Use the keywords "var," "let," or "const" to declare variables.

  2. "var" (older approach):

    • Variables declared with "var" have function scope or global scope.
    • They can be reassigned and updated.
    • Example: var age = 25;
  3. "let" (block-scoped):

    • Variables declared with "let" have block scope, meaning they are limited to the block (e.g., within curly braces) where they are declared.
    • They can be reassigned but not redeclared within the same scope.
    • Example: let name = "John";
  4. "const" (block-scoped constants):

    • Variables declared with "const" have block scope and cannot be reassigned.
    • They must be assigned a value when declared, and the assigned value remains constant.
    • Example: const PI = 3.14;
  5. Variable names:

    • Must start with a letter (a-z, A-Z), underscore (_), or dollar sign ($).
    • Subsequent characters can be letters, digits (0-9), underscores, or dollar signs.

Types of variables

There are two types of comments available in javascript:-

Undefined vs. undeclared variables

An undefined variable is a variable that has been declared. Because we have not assigned it a value, the variable used the undefined as its initial value.

var message;

console.log(“message”,message); //undefined variable

In contrast, an undeclared variable is a variable that has not been declared.

console.log(“counter”,counter); //undeclared variable

In the above examples, the message variable is declared but not initialized therefore its value is undefined whereas the counter variable has not been declared hence accessing it causes a ReferenceError.

Global vs. local variables

In JavaScript, all variables exist within a scope that determines the lifetime of the variables and which part of the code can access them.

A local variable is declared inside a block or function. It is accessible within the function or block only.

Input:-

Output:-

A global variable is accessible from anywhere in that execution context. A variable i.e. declared outside the function or declared with a window object is known as a global variable.

Input:-

Output:-

Variable shadowing

This is a concept in which variables share the same name within different scopes.

One variable overshadow the value of other variable. The following example will illustrate it.

Input:-

Output:-

In the above example, we have two variables that share the same name: x. The first variable is a global variable whereas the second one is the local variable.

Inside the getScope() function, the global variable is shadowed. It cannot be accessible inside the getScope () function but outside of the function. This is called variable shadowing.

Go back to Previous Course