Front end developer Front end developer - 1 year ago 72
Javascript Question

Global variable is null

I have a global variable. The variable is equal to null. " var a=null;" Then I write a=a+"example" in console. But output is nullexample. Why is it so ?

Answer Source

There are three possibilities in javascript:

Option 1

var a; 

output: "undefinedexample"

Option 2

var a=null; 

output: "nullexample"

Option 3

var a=""; 

output: "example"

As per your Question you need to define third option. bcz in javascript null and "" both are different types.

For more ref JavaScript String concatenation behavior with null or undefined values