Search This Blog

2026/08/10

Javascript:Guess output of given code

 


Guess what will be the output of the following code snippet:

Code:
    const num1 = Number();
    const num2 = Number(undefined);

    console.log('num1=',num1); // 0
    console.log('num2=',num2); // 0

Output:
    num1= 0
    num2= NaN

Explanation:
In JavaScript, when you call the `Number()` function without any arguments,
it returns `0`. However, when you pass `undefined` to the `Number()` function,
it returns `NaN` (Not-a-Number) because `undefined` cannot be converted to a
valid number.

No comments:

Post a Comment