Search This Blog

2024/03/26

Javascript : null & undefined

In JavaScript, null is a value that represents the intentional absence of any
object value. It is technically a primitive type, although in some cases
it behaves as an object.

undefined means the variable has not been assigned a value yet.

Checking for null:
You can check whether a value is null using the === operator:
Code:
var s= null;
if(s===null){
console.log('s is null')
}
Output:
s is null

If variable 's' is null or undefined then == return true.
Code:
if(s==null){
console.log('s is null or undefined')
}
Output:
s is null or undefined

null vs undefined:
undefined means the variable has not been assigned a value yet,
whereas null means the variable has been explicitly defined as null

null & undefined together termed as nullish value.

Nullish value vs Non-nullish value:
Nullish values are different from non-nullish values in that
nullish values throw a TypeError when you try to access one of
their properties, whereas non-nullish values do not.

Code:
let v = 42;
console.log(v.test); // undefined

v = null;
v.test; // Throws `TypeError: Cannot read property 'test' of null`

v = undefined;
v.test; // Throws `TypeError: Cannot read property 'test' of undefined`

Arithmetic Operations on null:
During mathematical operation javaScript converts null to 0.

consider following code.
Code:
console.log(2 + null); // 2
console.log(null + 2); // 2

console.log(2 - null); // 2
console.log(null - 2); // -2

console.log(2 * null); // 0
console.log(null * 2); // 0

console.log(2 ** null); // 1
console.log(0 ** 2); // 0

console.log(null / 2); // 0
Output:
2
2
2
-2
0
0
1
0
0

Arithmetic Operation on undefined:
Result of any arithmetic operation on undefined is always NaN.
Code:
console.log(2 + undefined); // NaN
console.log(2 * undefined); // NaN
console.log(2 - undefined); // NaN
console.log(2 ** undefined); // NaN
console.log(2 / undefined); // NaN

console.log(undefined + 2); // NaN
console.log(undefined - 2); // NaN
console.log(undefined * 2); // NaN
console.log(undefined / 2); // NaN
console.log(undefined ** 2); // NaN
Output:
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN

Applying typeof operator on null
Code:
var s = null
console.log(typeof s)
Output:
object

Why typeof null is an object?
It was a historial mistake.In the early versions of JavaScript, the
language used a 32-bit system to store values. These bits were divided
into two parts: the type tag and the data value.The type tag for objects
was 000, which corresponds to the binary value 000 in the least significant
bits.The type tag for null was also 000,leading to the confusion and the
typeof null returning "object".

Checking if variable is not null but object:
Code:
function isObject(v) {
return typeof v === "object" && v !== null;
}

var t = {
name: "sangram",
};

if(isObject(t)){
console.log('t is object')
}

var s = null
if(!isObject(s)){
console.log('s is not an object')
}

Output:
t is object
s is not an object
Explanation:
1)isObject() function return true if passed variable has not null value &
it's type is object.

2)isObject() function return false if passed variable has null value or
it's type not object.

Applying typeof operator on null
Code:
var t = undefined
console.log(typeof t)
Output:
undefined

Concatenation:

when we concat a variable with null value or variable with undefined value,
first toString() method is called on this variables.toString() for variable
with null value is 'null' string.toString() for variable with undefined
value is 'undefined' string.

string concatenation on null:
Code:
var s = null
console.log(s+ 'some value')
console.log('some value' + s)
Output:
nullsome value
some valuenull

string concatenation on undefined:
Code:
var s = undefined
console.log('some value' + s)
console.log(s + 'some value')
Output:
some valueundefined
undefinedsome value

Comparing two null variables:
Code:
var s =null;
var t =null;
if(s===t){
console.log("Equal")
}else{
console.log("Not Equal")
}
Output:
Equal

Comparing two undefined variables:
Code:
var s =undefined;
var t =undefined;
if(s===t){
console.log("Equal")
}else{
console.log("Not Equal")
}
Output:
Equal

No comments:

Post a Comment