Vaidikalaya

Node Variables


Variables are containers that are used to store different types of data. The variables can store character values, numeric values, memory addresses, and strings. Node.js uses JavaScript as its programming language, so variables in Node.js are handled the same way as in JavaScript. You use var, let, and const to declare variables.


Variable Declaration

In Node.js, declaring variables follows the same syntax and rules as in JavaScript. Here’s how you can declare variables using var, let, and const keywords

Example:

var a=10;
let b=20;
const c=30;
console.log("a: "+a);
console.log("b: "+b);
console.log("c: "+c);

Output:

a: 10
b: 20
c: 30

Screenshot


The difference between var, let, and const

1. Var Keyword

When declaring a variable with the var keyword, the scope of the variable is either global or function-scoped.

Global Scope: If var is declared outside of any function, the variable is globally scoped and can be accessed from anywhere in the entire script.

var a=10;
function test(){
    console.log('Inside Function: '+a);
}
test();
console.log('Outside Function: '+a);

/*
output:
Inside Function: 10
Outside Function: 10
*/


Function Scope: If var is declared within a function, the variable is function-scoped, meaning it can only be accessed within that function and any of its sub-functions.

function test(){
    var a=10;
    console.log('Inside Function: '+a); //output 10
}
test();
console.log('Outside Function: '+a); //output a is not defined

/*
Inside Function: 10
a is not defined
*/

Screenshot


2. Let Keyword

The let keyword in Node or JavaScript is used to declare variables that are limited in scope to the block, statement, or expression in which they are used. It was introduced in ECMAScript 2015 (ES6) as part of the new block-level scoping, providing a more intuitive and safer alternative to var, which is function-scoped or globally-scoped.

Examples:

let a=10;
if(a>5){
    let b=20;
    console.log(b); //output: 20
}
console.log(a) //output: 10
console.log(b); //output: error (b is not defined)

//b is defined inside the if block, So It is not accessible outside the block

let does not allow you to re-declare the same variable within the same scope. Attempting to do so will throw a syntax error, which helps in avoiding and identifying potential bugs related to repeated declarations.

let a=10;
let a=20; // SyntaxError: Identifier 'a' has already been declared

3. Const Keyword

The const keyword in Node or JavaScript is used to define variables that cannot be changed once they’re assigned a value. This prevents any modifications to the variable’s value. It was introduced in ES2015 (ES6) for creating immutable variables.

Examples:

⮚ Variables declared with const must be initialized at the time of their declaration.

const a=10;
console.log(a);
//output: 10

⮚ Once a variable is assigned a value using const, it cannot be reassigned to a different value. Attempting to do so will result in a TypeError.

const a=10;
a=20; // TypeError: Assignment to constant variable.

In Node.js, these variables are used for managing data just like in any JavaScript environment. But It’s recommended to use let and const for declaring variables to take advantage of block scoping, which can prevent many common bugs caused by the hoisting behavior of var.