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
| 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
| 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
| 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
| document.getElementById("myBtn").addEventListener("click", e => { | |
| //some code | |
| e.preventDefault(); //This specifically removes the default behavior of refreshing the page | |
| }) |
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 myArr = ["1", "2", "3"]; | |
| myArr.push("4"); | |
| console.log(myArr); |
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 = "mylowercasestring"; | |
| let cappedStr = str.charAt(0).toUpperCase() + name.slice(1)); | |
| console.log(cappedStr); //--> "Mylowercasestring" |
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
| //Get the EPOCH Time | |
| console.log(Date.now()) //--> 1567548304725 | |
| //Get a string date | |
| console.log((new Date).toLocaleString()); //--> 9/3/2019, 3:07:03 PM |
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
| function genUUID() { | |
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
| var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); | |
| return v.toString(16); | |
| }); | |
| } | |
| console.log(genUUID()) //--> 26a63d24-aef9-4047-9284-9fdb366bae95 |
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 checkBox = document.getElementById("myCheckBox"); | |
| console.log(checkBox.checked) //--> true or false |