javascript assignment operator if undefined

JavaScript Assignment Operator If Undefined

//The Assignment Operator in JavaScript
var x = 10; //Assigning the value of 10 to variable x.
var y = 20; //Assigning the value of 20 to variable y.

There are various assignment operators in JavaScript, like =, +=, -=, *= etc. The equal to (=) operator is used to assign a value to a variable.

If Undefined

var z; //Declaring a variable z without assigning any value.
console.log(z); //Outputs 'undefined'.

If you try to assign a variable that is undefined, and then try to use it, it will result in an error.

var a;
console.log(a); //Outputs 'undefined'.
a = a + 10; //This will result in an error.

In the above code snippet, variable a is declared without assigning any value. When we try to use it, it will return undefined. If we try to assign a value to it and use it in an expression, we will get an error because undefined cannot be used in any arithmetic operation.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]