27
loading...
This website collects cookies to deliver better user experience
let number = 98234567
let number = 98_234_567
const binary = 0b1000_0101;
const hex = 0x12_34_56_78;
let num= 0_12
let num= 500_
"By reducing your global footprint to a single name, you significantly reduce the chance of bad interactions with other applications, widgets, or libraries." - Douglas Crockford
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
Note that it is not in fact set to the value undefined, rather the property is removed from the array, making it appear undefined. The Chrome dev tools make this distinction clear by printing empty when logging the array.
Splice()
actually removes the element, reindexes the array, and changes its length.> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
The delete method should be used to delete an object property.
var squares = [1,2,3,4].map(function (val) {
return val * val;
});
// squares will be equal to [1, 4, 9, 16]
var pi =3.1415;
pi = pi.toFixed(2); // pi will be equal to 3.14
NOTE : toFixed()
returns a string and not a number.
console.table
to show objects in tabular format:table=[{state: "Texas"},{state: "New York"},{state: "Chicago"}]
console.table(table)
var object = ['foo', 'bar'], i;
for (i = 0, len = object.length; i <len; i++) {
try {
// do something that throws an exception
}
catch (e) {
// handle exception
}
}
var object = ['foo', 'bar'], i;
try {
for (i = 0, len = object.length; i <len; i++) {
// do something that throws an exception
}
}
catch (e) {
// handle exception
}
indexOf()
or includes()
method.if (value === 1 || value === 'one' || value === 2 || value === 'two') {
}
if ([1, 'one', 2, 'two'].indexOf(value) >= 0) {
}
if ([1, 'one', 2, 'two'].includes(value)) {
}
const floor = Math.floor(6.8); // 6
const floor = ~~6.8; // 6
The double NOT bitwise operator approach only works for 32-bit integers. So for any number higher than that, it is recommended to use Math.floor()
https://stackoverflow.com/questions/2485423/is-using-var-to-declare-variables-optional
Cover Photo by Juanjo Jaramillo on Unsplash