Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 23, 2022 04:52 pm GMT

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 functionconst sum = (a, b) => a + b// Regular Functionfunction sum(a, b) {    return a + b;}

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}

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

// beforeconst hello = "Hello"console.log(hello + " World")// afterconst hello = "Hello"console.log(`${hello} World`)

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 + cconst num = [4, 5, 6]console.log(`Sum: ${sum(...num)}`)

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"}

Now lets extract name and age property values and assign them to a variable.

const name = me.nameconst age = me.age

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} = meconsole.log(name, age)

Thank you for reading, have a nice day!
Your appreciation is my motivation


Original Link: https://dev.to/codewithsnowbit/why-you-should-write-clean-code-as-a-javascript-developer-5akn

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To