10 JavaScript Tricks You Should Know to Save Time and Effort



JavaScript is a full-fledged dynamic programming language that can add interactivity to a website. It is beginner-friendly and provides many tools that can save a lot of effort and time if used correctly. These are the 10 such tricks you should know while working with JavaScript. Read on for a list of time-saving tips.

1. Console Log like a Pro

When you debug your code, it’s common to log a value to check the assigned result. While you can always do it like below,

console.log('name', name)
console.log('someOtherValue', someOtherValue)
console.log('etc', etc)

you can use Object destructuring to make your life easier.

console.log({name, someOtherValue, etc})

2. Object Short-hand properties

Suppose you have initialized some values in variables x and y, and you want to create an object with the properties x and y. Using basic literal syntax, you’ll end up repeating each identifier

let x = 1, y = 2
const obj = {
   x: x,
   y: y
}

In ES6 and later, you can drop the colon and one copy of the identifier and end up with much simpler code:

let x = 1, y = 2
const obj = {
   x,
   y
}
obj.x + obj.y => 3

3. Logical Assignment Operators

You might be aware of the mathematical assignment operators. It lets you perform a mathematical operation on a variable with the value you are assigning.

let count = 1
count += 2 // 3
count -= 1 // 2

Suppose you’d want to assign a value to the variable if the current value of the variab