DEV Community

Dhairya Shah
Dhairya Shah

Posted on

4 1

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

ACI image

ACI.dev: Best Open-Source Composio Alternative (AI Agent Tooling)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Star our GitHub!

Top comments (1)

Collapse
 
amircahyadi profile image
Amir-cahyadi

❤️

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more