Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is string = '0' not strictly equal to new String('0') in javascript

Tags:

javascript

Why does the creation of a string object not return true when compared strictly to a primitive string value?

var obj = new String('0');
var str = '0';

console.log(obj == str); //returns true
console.log(obj === str); //returns false
like image 391
onTheInternet Avatar asked Jan 03 '23 18:01

onTheInternet


1 Answers

As obj type is object where str is string, Hence obj === str is false.

var obj = new String('0');
var str = '0';

console.log(typeof obj);
console.log(typeof str);
like image 199
Satpal Avatar answered Feb 01 '23 01:02

Satpal