console.log( ['a', 'b', 'c'].fill("") ); // Array [ "", "", "" ]
console.log( [1, 2, 3].find(x => x == 3) );
[6, 8, -5].findIndex(x => x < 0)
var str = 'Hello World!';
console.log(str.startsWith('Hello')); // True
var str = 'Hello World!';
console.log(str.endsWith('Hello')); // False
var str = 'Hello World!';
console.log(str.includes(' ')); // True
console.log( 'JS '.repeat(3) ); // JS JS JS
var arr = new Array([1,2,3]); // ES4.1
var arr1 = [1, 2, 3]; // ES4.1
let arr2 = Array.of(1, 2, 3); // ES6
console.log(arr); // 1,2,3
console.log(arr1); // 1,2,3
console.log(arr2); // 1,2,3
function a(){
return Array.from(arguments);
}
console.log( a(1, 2, 3)); // [1, 2, 3]
Number.isNaN(0 / 0); // true
Number.isNaN(NaN); // true
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isFinite(NaN); // false
Number.isFinite(0);
== and === both Operators are comparing same things, but == allows coercion and === disallows coercion, making === faster and more accurate.
Following best practices: *Avoid == potential coerce errors *Use === operator and also make it faster or Object.is()
console.log(1 == "1"); // true
console.log(1 === "1"); // false
Object.is(1,"1"); // false
console.log(NaN == NaN);
console.log(NaN === NaN); // false
Object.is(NaN,NaN); // true
console.log('' == 0); // true
console.log('' === 0); // false
console.log(Object.is('',0); // false
// doesn’t work on IE11
For merging objects we will have the function
var obj = { a: 1 };
var copy = Object.assign({}, obj); // Object.assign(target, source_1, ..., source_n)
console.log(copy); // { a: 1 }
let o1 = 'Jon';
let o2 = {[o1]: false,
['D'+'o'+'e']: null
};
console.log(JSON.stringify(o2));
let person = { name: 'Mr.' };
let name = 'Leo';
let tpl = `I'm: ${person.name} ${name}`; // we are using with `back-ticks` or `grave-accent`
console.log(tpl);
for-of loops ecma-international.org docs
In addition to the classic for-in statement, ES6 has added the for-of statement .
Strings are iterable, which means that you can use for-of to iterate over their characters While for-in iterates over "property names", for-of iterates over "property values"
for (let index in 'Jon Doe') { // property names
console.log(index); // 0,1,2,3,4,5,6
}
for (let index of 'Jon Doe') { // property values
console.log(index); // J,o,n,,D,o,e
}
var arr = [1, 2, 3];
for (var n of arr) console.log(n); // 1,2,3
Eg3: simple for-of loop throw Array
for (let index of 'Jon Doe') {
console.log(index);
}
let arr = ['Hello', 'world', "!"];
for (let element of arr) {
console.log(element);
}
for (let [index,element] of arr.entries()) {
console.log(`${index}. ${element}`);
}
Eg2: for-of with template literal
A new primitive type: Symbol() They are tokens that serve as unique IDs. This key avoid accidentally clash with any other property key:
let symbol1 = Symbol();
let symbol2 = Symbol();
console.log(typeof symbol1);
console.log( Object.is(symbol1,symbol2) );
let wrap = Object(symbol2);
console.log(typeof wrap);
console.log(wrap instanceof Symbol)
console.log('symbol2 symbol: ' + String(symbol2) );
You can specify the key of a property via an expression, by putting it in square brackets. In the following object literal, we use a computed property key to make the value of MY_KEY the key of a property.
const _id = Symbol(); // generate Unique token and cannot be changed
let obj = {
[_id]() { // specify the key of a property via an expression
return 'ID007';
}
};
console.log( obj[_id]() ); // ID007
var foo1 = function(n) {
n = n || "Leo";
console.log( n )
};
let foo2 = function(n = "Leo") {
console.log( n );
};
foo1(); // Leo
foo2(); // Leo