str.split('').reverse().join('')
let a = 'Happy New Year 2019!';
let b = 'Year 2019';
let c = 'Year 2018';
a.indexOf(b) >= 0 ? "Found" : "Not Found"; // return 10 (index of first element of substring in search string); -> "Found"
a.indexOf(c) >= 0 ? "Found" : "Not Found"; // return -1; -> "Not Found"
// f.e. we have date in string format:
let date = '20190211';
// but we want it like 2019-02-11
// String -> Array
date = date.split('');
date.splice(4,0,'-'); // return []; date = ["2", "0", "1", "9", "-", "0", "2", "1", "1"]
date.splice(date.length - 2, 0, '-'); // return []; date = ["2", "0", "1", "9", "-", "0", "2", "-", "1", "1"]
// Array -> String
date = date.join(''); // return "2019-02-11";
Note: Only arrays have method .splice() and strings are haven't;
newArray = [].concat(array);
Note: this case will works with plain arrays like [1,2,3,4,5] and will not work with arrays contains objects [{a:1},2,3]. Because objects in JS copying by reference and not by value.
[1, 2, 3].reduce((a,b) => a + b, 0);
function sum(a) {
return function(b) {
if (b == undefined)
return a;
else
return sum(a+b); // sum(1)(2)(3)(4)() === 1+2+3+4
};
}
let array_values = [1, 3, 3, 4, 5, 6, 6, 6, 8, 4, 1];
let unique_values = [...new Set(array_values)];
console.log(unique_values); // [1,3, 4, 5, 6, 8]
let arrValues = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(arrValues.length); // 8
arrValues.length = 5;
console.log(arrValues.length); // 5
console.log(arrValues); // [1, 2, 3, 4, 5]
Min:
[1,2,-3,0,12,9,5].reduce((a,b) => a < b ? a : b); // return -3;
Max:
[1,2,-3,0,12,9,5].reduce((a,b) => a > b ? a : b); // return 12;
5..toString(2); // return "101";
or
Number("5").toString(2); // return "101";
Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase();
let arr = [1,2];
arr.push(3); // return 3; arr=[1,2,3]
arr.pop(); // return 3; arr=[1,2]
let arr = [1,2];
arr.push(3); // return 3; arr=[1,2,3]
arr.shift(); // return 1; arr=[2,3]