Triple equals (===) checks for both value and type equality, sparing you from unexpected bugs due to type coercion.
3 === "3" // false because different types
3 == "3" // true because JS converts strings to numbersUtilize ternary operators for simple if-else conditions.
It makes your code more concise and easier to read.
var age = 15;
var beverage = (age >= 21) ? "Coffee" : "Juice";
console.log(beverage); // Output: JuiceWrite async behavior with Promises and async/await to avoid callback hell.
async function fetchData() {
var response = await fetch('API_URL');
var data = await response.json();
console.log(data);
}Take advantage of Array methods like .map(), .filter(), and .reduce() for efficient manipulation.
var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]Use JSON.stringify() and JSON.parse() for easy conversion between JSON and JS objects!
var person = { name: "John", age: 30 };
var json = JSON.stringify(person);
var obj = JSON.parse(json);Shorten your code. Use the shorthand for if statements.
Take note of the code given below.
let isReady = true;
if (isReady) console.log('Ready');
// Can be shortened to
isReady && console.log('Ready');Destructuring in JavaScript is a total game-changer!
It allows you to extract data from arrays or objects into distinct variables. Check out the example below:
let [name, age] = ['John', 25];
console.log(name); // Output: JohnThat's pretty much it for today. I really hope you find this thread helpful. Thank you for reading!