-2

Why is it in JavaScript that null+'' === 'null' ?

At least this is the case on Chrome 68.

It doesn't make a sense to me. Different languages handle it differently of course, but I would assume either null or '' to be a better answer.

It just makes it hard to check if something is 'null' or if it is null + ''. Which could happen in a text field for example.

This is particularly an issue with ECMAScript 6 where you can do

value={\`${value}`}

etc.

1
  • To answer in simple language, + operator does 2 things, add or concat. if one of value is not getting converted to int, it converts both values to string using internal string function. For sample you can try String(null); This returns you "null" and hence it is equal. Hope that addresses your query Commented Sep 5, 2018 at 6:01

1 Answer 1

0

By doing console.log(null+'') which is adding an empty string to null, you are just converting null to a string.

Look at some of the examples below:

var first = 100 + '';
var second = 10 + '' + 2;
var third = undefined + '';
var fourth = null + '';

console.log(typeof first);
console.log(typeof second);
console.log(typeof third);
console.log(typeof fourth);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.