JavaScript : Variables

In JavaScript, variables are used to store and manipulate data. They are like containers that hold values, and the values stored in variables can change during the execution of a script. Here are the basics of working with variables in JavaScript:

1. Variable Declaration:


Variables are declared using the var, let, or const keywords.

  1. var (Function-Scoped): Declares a variable that is function-scoped.


  var age = 30;
  var name = "John";
 

  • let (Block-Scoped, Introduced in ES6): Declares a block-scoped variable. It is preferred over var for most use cases.


  let count = 10;
  let message = "Hello";
 

  • const (Block-Scoped Constant): Declares a constant, which cannot be reassigned. The value must be assigned when declaring a const.

 
  const PI = 3.14;
  const MAX_SIZE = 100;

  

2. Variable Naming Rules:

 

  • Variable names must begin with a letter (a-z, A-Z), underscore (_), or dollar sign ($).
  • Subsequent characters can be letters, digits (0-9), underscores, or dollar signs.
  • Variable names are case-sensitive ( name and Name are different).

3. Assigning Values:


You can assign values to variables using the assignment operator ( ).


let age = 30;
let name = "John";

 

 4. Data Types:


JavaScript is a dynamically typed language, meaning variables can hold values of any data type.

Primitive Data Types:

  •   Number: Floating-point numbers, like 3.14 or 10.
  •   String: Textual data, like "Hello".
  •   Boolean: Logical values, true or false.
  •   Undefined: Represents a variable that has been declared but not assigned a value.
  •   Null: Represents the intentional absence of any object value.
  •   Symbol: Introduced in ES6, unique and immutable data type.

Complex Data Types:

  •   Object: Key-value pairs, like `{ name: "John", age: 30 }`.
  •   Array: Ordered list of values, like `[10, 20, 30]`.

Example Usage:

Here's an example demonstrating variable declaration and assignment:


// Declaring and assigning values to variables
let firstName = "John";
let age = 30;
let isStudent = false;

// Changing the value of a variable
age = 31;

// Outputting variables to console
console.log("Name:", firstName);  // Output: Name: John
console.log("Age:", age);         // Output: Age: 31
console.log("Is Student:", isStudent);  // Output: Is Student: false

 

Scope:

 

  • Global Scope: Variables declared outside any function have global scope and can be accessed from anywhere in the script.  
  • Function Scope: Variables declared inside a function are local to that function and cannot be accessed from outside.
  • Block Scope (with let and const): Variables declared with let and const have block scope, meaning they are only accessible within the block they are defined in (inside curly braces {}).

Hoisting:

 

  • Variables declared with var are hoisted to the top of their scope during execution. This means they can be used before they are declared (though their value will be undefined until the assignment).
  • Variables declared with let and const are also hoisted, but they are not initialized (you'll get a ReferenceError if you try to access them before their declaration).

Conclusion:


Understanding variables is fundamental in JavaScript programming. They allow you to store and manipulate data throughout the execution of your scripts. Remember to choose the appropriate type ( var, let, const ) based on the scope and mutability requirements of your variables.