repeat()
console.log("x".repeat(3));// "xxx"console.log("hello".repeat(2));// "hellohello"
Object.is()
When you want to compare two values, you’re probably used to using either the equals operator (
==) or the identically equals operator (===). Many prefer to use the latter to avoid type coercion during the comparison. However, even the identically equals operator isn’t entirely accurate. For example, the values +0 and -0 are considered equal by === even though they are represented differently in the JavaScript engine. Also NaN === NaN returns false, which necessitates using isNaN() to detect NaN properly.
In many cases,
Object.is() works the same as ===. The only differences are that +0 and -0 are considered not equivalent and NaN is considered equivalent to NaN. Here are some examples:console.log(+0==-0);// trueconsole.log(+0===-0);// trueconsole.log(Object.is(+0,-0));// falseconsole.log(NaN==NaN);// falseconsole.log(NaN===NaN);// falseconsole.log(Object.is(NaN,NaN));// trueconsole.log(5==5);// trueconsole.log(5=="5");// trueconsole.log(5===5);// trueconsole.log(5==="5");// falseconsole.log(Object.is(5,5));// trueconsole.log(Object.is(5,"5"));// false
No comments:
Post a Comment