ref – https://medium.com/@_bengarrison/javascript-es6-exploring-the-new-built-in-methods-b62583b0a8e6
https://stackoverflow.com/questions/33164725/confusion-between-isnan-and-number-isnan-in-javascript
https://ponyfoo.com/articles/es6-number-improvements-in-depth
Use Object.assign to assign properties of objects over to your destination object.
1 2 3 4 5 6 7 8 9 10 |
console.log("---- built-in objects ----- "); var destination = {a:0}; var src1 = {b:1, c:2}; var src2 = {d:3, e:4}; console.log("---- object assignment ----- "); Object.assign(destination, src1, src2); console.log(destination); // { a: 0, b: 1, c: 2, d: 3, e: 4 } |
Using Object.assign, if there already exist a property/value, and you assign a new property that has the same name over, then the existing property/value gets replaced.
1 2 3 4 5 6 7 8 9 10 |
console.log("---- Merge With Same Properties ----- "); // notice overlapping properties b, and c. var o1 = {a:1, b:1, c:1}; var o2 = {b: 2, c: 2}; var ans = Object.assign( {}, o1, o2); console.log(ans); // { a: 1, b: 2, c: 2 } // the old properties gets replaced |
Using Object.assign, we can also do a deep copy of an object.
1 2 3 4 5 6 |
console.log("---Object clone---"); var objX = {a:1}; var copy = Object.assign({}, objX); objX.a = 4; console.log(objX); // 4 console.log(copy); // 1 |
find and findIndex both find elements for you. It is important to note that find() will return the FIRST element in the Array that satisfies the provided testing function.
Use find to see if ‘d’ exists.
1 2 3 4 5 6 7 8 9 10 |
console.log("-- Array Element Finding --"); var searchArray = ['a', 'b', 'c', 'c', 'd', 'e']; let res = searchArray.find( function(x) { console.log("find - looping: " + x); return x =='d'; // return if x equsl 'd' }); console.log("res: " + res); |
find – looping: a
find – looping: b
find – looping: c
find – looping: c
find – looping: d
res: d
Use findIndex to see if ‘c’ exists.
1 2 3 4 5 6 |
let res2 = searchArray.findIndex(function(x){ console.log("findIndex - looping: " + x); return x == 'c'; }); console.log(res2); |
findIndex – looping: a
findIndex – looping: b
findIndex – looping: c
2
1 2 3 |
console.log("--- string repeating---"); var repeat = "foo".repeat(3); console.log(repeat); // foofoofoo |
1 2 3 4 5 6 |
console.log(" --- string searching --- "); console.log("hello".startsWith("lo", 3)); // true console.log("hello".startsWith("e", 1)); // true console.log("hello".endsWith("h", 1)); // true console.log("hello".includes("a")); // false console.log("hello".includes("llo")); // true |
Number.isNaN
The reason isNaN() is “broken” is because, ostensibly, type conversions aren’t supposed to happen when testing values.
That is the issue Number.isNaN() is designed to address.
In particular, Number.isNaN() will only attempt to compare a value to NaN if the value is a number-type value.
1 |
console.log(Number.isNaN(42)); // value is a number-type value. 42 !== NaN, false. |
Any other type will return false, even if they are literally “not a number”, because the type of the value NaN is number.
1 2 3 4 |
console.log(Number.isNaN("hadooken!!")); //false, because type string console.log(Number.isNaN("42")); // false, because type string console.log(Number.isNaN(NaN)); // true |
You can also think of it as anything that’s not NaN when passed to Number.isNaN will return false, while passing NaN into it will yield true.
1 2 3 4 5 6 7 8 9 10 |
Number.isNaN(123) // <- false, integers are not NaN Number.isNaN(Infinity) // <- false, Infinity is not NaN Number.isNaN('ponyfoo') // <- false, 'ponyfoo' is not NaN Number.isNaN(NaN) // <- true, NaN is NaN Number.isNaN('pony'/'foo') // <- true, 'pony'/'foo' is NaN, NaN is NaN |
The ES5 global.isNaN method, in contrast, casts non-numeric values passed to it before evaluating them against NaN, using Number(…)
That produces significantly different results. The example below produces inconsistent results because, unlike Number.isNaN, isNaN casts the value passed to it through Number first.
Number.isNaN will return false immediately if the argument is not a Number
while isNaN first converts the value to a Number
1 2 3 4 5 6 7 8 9 |
Number.isNumber() isNaN() ----------------+----------------------------+----------------------- value | value is a Number | result | Number(value) | result ----------------+-------------------+--------+---------------+------- undefined | false | false | NaN | true {} | false | false | NaN | true "blabla" | false | false | NaN | true new Date("!") | false | false | NaN | true new Number(0/0) | false | false | NaN | true |
For isNaN():
1 2 3 4 5 |
Number(undefined) // NaN Number({}) // NaN Number("blabla") // NaN Number(new Date("!")) // NaN Number(new Number(0/0)) // NaN |
then it does a NaN === NaN comparison, which results to true.
for Number.isNumber() , as mentioned earlier, we check to see the type of the argument is a Number. If its not a Number, it returns false immediately!
Infinity is actually NOT infinity, it a numeric value that represents Infinity. The actual value is 1.797693134862315E+308. The same for -Infinity, -1.797693134862315E+308
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
console.log("--- Number.isFinite ---"); console.log(Number.isFinite(123)); // true console.log(Number.isFinite(Infinity)); // false console.log(Number.isFinite(-Infinity)); // false console.log(Number.isFinite(0)); // <- true console.log(Number.isFinite(null)); // <- false console.log(Number(null)); // 0 console.log(isFinite(null)); // true, because nul gets converted to Number firstName // absolute max number that "represents" infinity. console.log(Number.isFinite(1.797693134862315E+308)); // anything greater would get 'false' for isFinite console.log(Number.isFinite(-1.797693134862315E+308)); // anything less would get 'false' for isFinite console.log("--- math sign ---"); console.log(Math.sign(7)); // 1 console.log(Math.sign(-7)); // -1 console.log(Math.sign(0)); // 0 console.log(Math.sign(-0)); // -0 console.log(Math.sign(88)); // 1 console.log(Math.sign(-88)); // -1 |