Vaidikalaya

Node Variable Test Cases


Writing test cases for Node.js variables mainly involves ensuring that the variables behave as expected across different scenarios. This includes checking their scope, type, immutability, and reassignment behaviors.

Below, I'll provide some test cases that will help you identify bugs in your code. This will ensure that your variables are declared correctly and function as intended within your application.


Test Case 1: Hoisting

In JavaScript, "hoisting" means that variable declarations (not initializations) are moved to the top of their scope before the code is run. However, how it happens varies depending on whether you use var, let, or const to declare your variables.

With Var Keyword
console.log(a);
var a=10;
//output: undefined

In this scenario the declaration of 'a' is moved to the top, the part where a gets its value (a = 10) does not move. So, what happens when the code runs is similar to this:

var a;
console.log(a);
a=10;

When console.log(a) is executed, 'a' has been declared but has not yet been given its value of 10, so it shows as undefined.

Note: When you use var, the variable is declared at the top of its scope and automatically initialized to undefined. This means you can use the variable before you even declare it in your code.

With let and const Keyword

let and const also get hoisted, which means their declarations are moved to the top of their scope. However, they are not initialized to undefined automatically, which leads to a unique situation. It gives a ReferenceError because the variable is in the TDZ (Temporal Dead Zone).

console.log(a);
let a=10;
//output: ReferenceError: Cannot access 'a' before initialization
console.log(a);
const a=10;
//output: ReferenceError: Cannot access 'a' before initialization

So, while all three are hoisted, let and const provide a safer way to handle variables because they ensure you can't use a variable before you've properly declared it, thanks to the Temporal Dead Zone.


2. Scope of Variables

With Var keyword

var a=10;
if(a>5){
    var b=20;
}
console.log(a) //output: 10
console.log(b) //output: 20

In JavaScript, variables declared with the var keyword are function-scoped, which means they are accessible within the function in which they are declared, or globally if they are not declared inside a function. In the above example, there is no function wrapping the variable declarations, so both a and b are scoped to the global context.

Now see the below example

var a=10;
function test(){
    var b=20;
    console.log(a) //output: 10
    console.log(b) //output: 20
}
test();
console.log(a) //output: 10
console.log(b) //output: ReferenceError: b is not defined

The variable 'a' is declared outside of the function, so it is globally accessible throughout the code. On the other hand, 'b' is declared inside the function test(), so it is confined to the function scope and is only accessible within that function. So, when you try to use b outside its function, the program doesn't recognize it and gives an error because it only "lives" inside the function.

With Let and Const

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

In this example, the variable 'a' is globally accessible because it's declared outside of the block (if statement). While 'b' is declared inside the block (if statement) with 'let', So it can only be used inside that block (if statement). leading to a ReferenceError when accessed outside of its scope.

The 'const' keyword in JavaScript or Node.js behaves similarly to 'let' in terms of scoping. The example below has a similar structure to your previous example using 'let'.

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

3. Variable declaration and initialization

⮚ you can redeclare a variable using the var keyword.

var a=20;
var a=30;
console.log(a); //output: 30

⮚ Unlike var, let and const do not allow you to redeclare the same variable within the same scope

let a=20;
let a=30;
console.log(a);
//output: SyntaxError: Identifier 'a' has already been declared

⮚ Can't reassign value with const keyword

const a=20;
a=30;
console.log(a);
//output: TypeError: Assignment to constant variable.

⮚ When you declare an object with the const keyword, you can modify the properties of the object, but you cannot reassign the object to a new one.

const student={name:'Ram',age:22};
student.age=24;
console.log(student);
//output: { name: 'Ram', age: 24 }

But when you reassign the whole object then it is not possible

const student={name:'Ram',age:22};
student={name:'Raja',age:25};
console.log(student);
//output: TypeError: Assignment to constant variable.

Input/Output

⮚ I/O 1.

var a=20;
let a=30;
console.log(a);
//output:SyntaxError: Identifier 'a' has already been declared

⮚ I/O 2.

var a=20;
function test(){
    let a=30;
}
test();
console.log(a);
//output: 20

⮚ I/O 3.

var a=20;
function test(){
    var a=30;
}
test();
console.log(a);
//output: 20