Skip to content

Latest commit

 

History

History
75 lines (55 loc) · 1.94 KB

File metadata and controls

75 lines (55 loc) · 1.94 KB

7 Must-Know JavaScript Tips 🚀

Tip 1: Use '===' instead of '=='

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 numbers

Tip 2: Ternary operators

Utilize 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: Juice

Tip 3: Promises and async/await

Write 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);
}

Tip 4: Array methods

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]

Tip 5: JSON.stringify() and JSON.parse()

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);

Tip 6: if statements shorthand

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');

Tip 7: Destructuring

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: John

That's pretty much it for today. I really hope you find this thread helpful. Thank you for reading!