Over the years of programming, the way code is written has always been a priority, not only to keep the code clean, but also because it helps speed up development and collaboration and reduces bugs.

The same implementation requirements, constantly optimize and simplify the code, summarize experience, form skills, and constantly improve the quality of writing code.

You have to be very careful when programming, because “dirty” code can be a serious problem in large projects.

Many programmers criticize JavaScript languages for not having a standard implementation like Java or C++, but the fact is that JavaScript is one of the best languages in use today, and some of them are Facebook and Netflix. Libraries like React improved front-end performance and, on the back end, nextJs was used for speed, a combination that drives today’s programmers crazy.

ECMAScript V6 (abbreviated ES6 or ES2015) is the standard that JavaScript has been following since June 2015. Until then, we’ll use the JS version in browsers and Node.js.

Once again, you can improve the way you write code by learning the following tips.

1. Constant definition is usedconstInstead ofvar

Constants are variables that never change, and declaring variables in this way ensures that they never change and avoids bugs caused by variable changes.

/* old way */ var I = 1; /* const I = 1;Copy the code

2. Use for variable changesletInstead ofvar

The let statement declares a locally scoped block-scoped variable that will change:

Var myVal = 1; for (var i; i < 10; i++){ myVal = 1 + i; } /* let myVal = 1; for (let i; i < 10; i++){ myVal += i }Copy the code

3. Object declaration

Declare objects using shortcuts:

/* Old way: the Object() class made an unnecessary function call */ const myObject = new Object(); /* const myObject = {};Copy the code

4. Array declaration

Declare an array using a shortcut:

/* The old way: Array() class made an unnecessary function call */ const myArray = new Array(); /* const myArray = [];Copy the code

5. String concatenation

The following example of concatenating strings is very useful:

/* Old way */ const myStringToAdd = "world"; const myString = "hello " + myStringToAdd; /* const myStringToAdd = "world"; const myString = `hello ${myStringToAdd}`;Copy the code

6. Use shorthand for object methods

Examples of useful objects in:

/* Const customObject = {val: 1, addVal: function () {return CustomObject.val + 1; } /* Const customObject = {val: 1, addVal(){return CustomObject.val ++}}Copy the code

Create an object value

Example of creating a new object with a value:

/* const value = 1; Const myObject = {value: value} /* const value = 1; const myObject = { value }Copy the code

Assign a value to an object

An example of assigning an object value to another object:

/* const object1 = {val: 1, b: 2}; let object2 = { d: 3, z: 4 }; object2.val = object1.val; object2.b = object1.b; /* const object1 = {val: 1, b: 2}; const object2 = { ... object1, d: 3, z: 4 }Copy the code

9. Assign an array

A practical example of allocating values in an array:

/* const myArray = []; myArray[myArray.length] = "hello world"; /* const myArray = []; myArray.push('Hello world');Copy the code

Array concatenation

Example of a concatenated array:

/* void */ const array1 = [1,2,3,4]; Const array2 =,6,7,8 [5]; const array3 = array1.concat(array2); /* const array1 = [1,2,3,4]; Const array2 =,6,7,8 [5]; const array3 = [...array1, ...array2]Copy the code

Get multiple properties of the object

Example of creating a function with object arguments:

Function getFullName(client){return '${client.name} ${client.last_name}'; Function getFullName({name, last_name}){return '${name} ${last_name}'; / / function getFullName({name, last_name}){return '${name} ${last_name}'; }Copy the code

Get the value of the object

Get values and create variables:

/* const object1 = {a: 1, b: 2}; const a = object1.a; const b = object1.b; /* const object1 = {a: 1, b: 2}; const { a, b } = object1;Copy the code

Create a function

Examples of shortcut functions with arrows:

/* Const myFunct = function() {} /* const myFunct = function() {} /* const myFunct = () => {}Copy the code

14. Default values

Examples of setting a default value in a function argument:

/* const myFunct = (a, b) => {if (! a) a = 1; if (! b) b = 1; return { a, b }; } /* const myFunct = (a = 1, b = 1) => {return {a, b}; }Copy the code

15, use,reduceInstead offorEachandforTo sum

Example of finding the sum of an array:

/* const values = [1, 2, 3, 4, 5]; let total = 0; Values. ForEach ((n) => {total += n}) /* const values = [1, 2, 3, 4, 5]; let total = 0; for (let i; i < values.length; i++){ total += values[i]; } /* const values = [1, 2, 3, 4, 5]; const total = values.reduce((total, num) => total + num);Copy the code

16, array elements exist

/ * way * / const myArray on = [{a: 1}, {2} a:, {3} a:]; let exist = false; ForEach (item => {if (item.a === 2) exist = true}) /* const myArray = [{a: 1}, {a: 2}, {a: 3}]; const exist = myArray.some( item => item.a == 2)Copy the code

17, if Boolean shortcut

/* const a = 1; const b = 1; Let isTrue = false if (a === b) {isTrue = true} /* const a = 1; const b = 1; const isTrue = a === bCopy the code

18. Shortcuts to if values

/* const a = 5; let b; if (a === 5){ b = 3; } else { b = 2; } /* const a = 5; const b = a === 5 ? 3:2;Copy the code