This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { key: undefined } | |
// Check if the key is defined (warning: key could exist but set to undefined) | |
obj["key"] !== undefined; | |
//Check if the key is defined in the object (includes if set to undefined) | |
"key" in obj; | |
//Check if the key doesn't exist? | |
!("key" in obj); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { key: undefined } | |
// Check if the key is defined (warning: key could exist but set to undefined) | |
obj["key"] !== undefined; //false, but key still exists | |
//Check if the key is defined in the object (includes if set to undefined) | |
"key" in obj; // true, regardless of actual value | |
//Check if the key doesn't exist? | |
!("key" in obj); // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { first: "John", last: "Doe" }; | |
Object.keys(obj).forEach(function(key) { | |
console.log(key, obj[key]); // first, John | |
// last, Doe | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Change the class altogether? | |
document.getElementById("MyElement").className = "MyClass"; | |
// Add to the class? | |
document.getElementById("MyElement").className += "MyClass"; | |
// Add or Remove a new class? | |
document.getElementById("MyElement").classList.add('MyClass'); | |
document.getElementById("MyElement").classList.remove('MyClass'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ES 7+ | |
Object.entries(obj).length === 0 && obj.constructor === Object | |
//ES 5+ | |
Object.keys(obj).length === 0 && obj.constructor === Object | |
//ES 5- | |
function isEmpty(obj) { | |
for(var prop in obj) { | |
if(obj.hasOwnProperty(prop)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = ["7", "2", "5", "9"]; | |
// Sort in ascending order? | |
arr.sort(); // --> 2,5,7,9 | |
// Descending order? | |
arr.reverse(); // --> 9,7,5,2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arr = [ | |
{ prop: "7" }, | |
{ prop: "3" }, | |
{ prop: "6" }, | |
{ prop: "1" } | |
] | |
//Sort in ascending | |
arr.sort(function(a, b) { | |
return (a.prop - b.prop); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let idLength = 6; | |
Math.random().toString(36).slice(idLength); // y80wde7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="https://google.com" target="_blank">GO TO GOOGLE.COM IN SEPARATE WINDOW</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "Masautt"; | |
let subString = "sau"; | |
console.log(str.includes(subString)); // true |