I saw a question here -> https://github.com/lydiahallie/javascript-questions#answer-c-1
Consider the below code snippet.
let a = 3;
let b = Number(3)
let c = new Number(3)
a == b // true
a === b // true
// but
a===c or b === c // false
The situation is also explained in the above repo, but I would like to know what are the other features of this c object.
I also tried to see its properties or methods in the browser console, and I found that it is the same as a, the methods are toFixed, etc.
new Number creates a wrapper object.
To get its primitive value, you can use Number#valueOf:
let a = 3, b = Number(3), c = new Number(3);
console.log(typeof c);
console.log(a === c.valueOf());
console.log(b === c.valueOf());
Because
let a = 3; // is a number
let b = Number(3) // is a number
let c = new Number(3) // is an object
a == b // true
a === b // true
// but
a===c or b === c // false
Is that clear?
When you check the typeof all the values then you will get
typeof a => number
typeof b => number
typeof c => object
=== is strict equality, so it will check its type and value
1) If you want to check equality either you can use a == c and b == c .
2) use the valueOf method of c and compare it with a and b as:
a === c.valueOf();
b === c.valueOf();
let a = 3;
let b = Number(3);
let c = new Number(3);
console.log(typeof a === "number"); // true
console.log(typeof b === "number"); // true
console.log(typeof c === "number"); // false
console.log(typeof c === "object"); // true
console.log(a == c);
console.log(b == c);
console.log(a === c);
console.log(b === c);
console.log(a === c.valueOf());
console.log(b === c.valueOf());
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }