DEV Community

Dhairya Shah
Dhairya Shah

Posted on • Originally published at codewithsnowbit.hashnode.dev

Why you should write clean code as a JavaScript Developer?

Hello Folks ๐Ÿ‘‹

What's up friends, this is SnowBit here. I am a young passionate and self-taught developer and have an intention to become a successful developer.

Today, I am here with something important for you as a JavaScript Developer.

Why you should write clean code as a JavaScript Developer

Writing clean code improves the maintainability of the application and make the developer productive. Unfortunately, some developers are unaware of this language feature.

๐ŸŒŸ Make Use of Arrow Functions

Arrow functions provide the abridged way of writing JavaScript.

The main benefit of using arrow functions in JavaScript is curly braces, parenthesis, function, and return keywords become completely optional; and that makes your code more clear understanding.

The example below shows a comparison between the single-line arrow function and the regular function.

// single line arrow function
const sum = (a, b) => a + b

// Regular Function
function sum(a, b) {
    return a + b;
}

Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ Use Template Literals for String Concatenation

Template literals are determined with backticks

Template literals can contain a placeholder, indicated by a dollar sign and curly braces

    ${expression}
Enter fullscreen mode Exit fullscreen mode

We can define a placeholder in a string to remove all concatenations.

// before
const hello = "Hello"
console.log(hello + " World")

// after
const hello = "Hello"
console.log(`${hello} World`)

Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ Spread Syntax

Spread Syntax(...) is another helpful addition to ES6.

It is able to expand literals like arrays into individual elements with a single line of magic code. ๐Ÿ”ฎ

const sum = (a, b, c) => a + b + c
const num = [4, 5, 6]
console.log(`Sum: ${sum(...num)}`)

Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ Object Destruction

Object destruction is a useful JS feature to extract properties from objects and bind them to variables.

For example, here we create an object with curly braces and a list of properties.

const me = {
    name: "SnowBit",
    age: 15,
    language: "JavaScript"
}

Enter fullscreen mode Exit fullscreen mode

Now letโ€™s extract name and age property values and assign them to a variable.

const name = me.name
const age = me.age

Enter fullscreen mode Exit fullscreen mode

Here, we have to explicitly mention the name and age property with me object using dot(.), and then declare the variables and assign them.

We can simplify this process by using object destruction syntax.

const {name, age} = me
console.log(name, age)

Enter fullscreen mode Exit fullscreen mode

Thank you for reading, have a nice day!
Your appreciation is my motivation ๐Ÿ˜Š

Top comments (0)