DEV Community

Dhairya Shah
Dhairya Shah

Posted on

3 Clever JavaScript Tricks for Beginners

Hello, I am back again with a new article for mostly beginners and if you are intermediate or at advanced level than this article might be useful for them also.

Let's get's started:

1. Pass arguments to functions as objects

const product = ({name, description, price}) => {
    console.log("Name: ", name)
    console.log("Description: ", description)
    console.log("Price: ", price)
}

console.log(product({
    name: "Soda",
    description: "Water with CO2",
    price: "$0.5"
}))
Enter fullscreen mode Exit fullscreen mode

Some benefits of using,

  • Auto Complete becomes easier as IDE will focus on arguments only.
  • Easy to handle large codebase
  • Clear way to call functions

2. Format JSON Data with spaces


const product = {
    name: "Soda",
    description: "Water with CO2",
    price: "$0.5"
}

const formatted = JSON.stringify(product, null, 2)
console.log(formatted)
//{
//  "name": "Soda",
//  "description": "Water with CO2",
//  "price": "$0.5"
//}
Enter fullscreen mode Exit fullscreen mode

Use spread operators to copy objects

const num = [1,2,3,4,5,6,7,8,9]

const spaceOperator = [...num, 10, 11, 12]

console.log(spaceOperator)
Enter fullscreen mode Exit fullscreen mode

Make sure to check out my Youtube Channel and subscribe to it

Top comments (1)

Collapse
 
amircahyadi profile image
Amir-cahyadi

❤️