By Apoorv Tyagi
Have a dream, have dry goods, wechat search [big Move the world] pay attention to this in the early morning is still in the bowl washing wisdom.
In this paper, making github.com/qq449245884… Has been included, a line of large factory interview complete test sites, information and my series of articles.
Let’s cut to the chase and arrange it.
1. Use numeric delimiters
This is one of the most common operators I use when I need to deal with large numbers. When you use a delimiter (just one _) in a number, it looks better than an undelimited number. Such as:
let number = 98234567
Copy the code
You could write it this way
let number = 98_234_567
Copy the code
And it can also be applied to any other base number.
const binary = 0b1000_0101;
const hex = 0x12_34_56_78;
Copy the code
A few notes:
Cannot be used after leading 0.
let num= 0_12
Copy the code
Do not appear at the end of a number.
let num= 500_
Copy the code
2. Always use a semicolon
Terminating a line with a semicolon is a good practice. If you forget it, you will not be warned, because in most cases it will be inserted by the JavaScript parser, but relying on automatic semicolon insertion (ASI) is discouraged.
The JS style guides of Google, Airbnb, and jQuery also recommend using semicolons to terminate lines.
Don’t forget var
When you first assign to a variable, make sure you do not assign to an undeclared variable.
Assignment to an undeclared variable automatically results in the creation of a global variable. Avoid global variables ❌
Global variables can easily be overwritten by other scripts. For example, if two separate parts of your application define global variables with the same name but different uses, unpredictable errors can result, and debugging such problems can be a scary experience.
Normally variables in a function should be local so that they are released when you are done executing the function.
4. Delete vs Splice
Use splice instead of delete to remove an item from an array. Using DELETE deletes the attributes of the object, but does not reset the indexed array or update its length.
Delete
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
Copy the code
Notice that it is not actually set to the value of undefined, but rather removes the property from the array to make it look undefined. And you can see that in control by printing myArray out.
Splice
Splice() actually deletes elements, resets indexes, and changes the length of the array.
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
Copy the code
The delete method should be used to delete object properties.
5. map vs for loop
Use the map() function to iterate over the items of the array
Var squares = [1,2,3,4]. }); // [1, 4, 9, 16]Copy the code
**Immutability ** – The original array will not be affected. This is beneficial in cases where raw arrays are still needed elsewhere. A for loop could certainly be implemented, but this would require more code and would require updating our new array as part of the loop operation. Map (), on the other hand, keeps this clean because you only need to work within an action potential and still remain immutable.
Cleaner Code – Map can almost always be written in less code than for when doing the same thing. It can sometimes be written clearly on one line, whereas for takes at least two lines or generally three, and includes parentheses. In addition, scoping isolation and reducing the number of variables you need, as well as the size you need, make your code objectively cleaner.
6. Rounded numbers
The toFixed() method uses fixed-point notation to format a number.
Var PI = 3.1415; pi = pi.toFixed(2); // PI will be equal to 3.14Copy the code
Note :toFixed() returns a string, not a number.
7. Use the console. The table
table=[{state: "Texas"},{state: "New York"},{state: "Chicago"}]
console.table(table)
Copy the code
You can use console.table to display objects as tables.
8. Avoid using in loopstry-catch
Each time a catch clause is executed, the try-catch structure creates a new variable in the current scope, where the caught exception object is assigned to a variable.
var object = ['foo', 'bar'], i;
for (i = 0, len = object.length; i <len; i++) {
try {
// do something that throws an exception
}
catch (e) {
// handle exception
}
}
Copy the code
The second way to write it
var object = ['foo', 'bar'], i;
try {
for (i = 0, len = object.length; i <len; i++) {
// do something that throws an exception
}
}
catch (e) {
// handle exception
}
Copy the code
When an error occurs, the first lets you continue the loop and the second exits the loop. If your code doesn’t throw an exception serious enough to stop the whole program, then the first one works.
9. Multiple condition checks
For multi-value matching, we can put all values in an array and use indexOf() or includes() methods.
if (value === 1 || value === 'one' || value === 2 || value === 'two') {
}
Copy the code
indexOf():
if ([1, 'one', 2, 'two'].indexOf(value) >= 0) {
}
Copy the code
includes():
if ([1, 'one', 2, 'two'].includes(value)) {
}
Copy the code
10. Double nonbit operator (~~)
The two-nonbit operator can be considered an alternative to the math.floor () method.
Const floor = Math. Floor (6.8); / / 6Copy the code
You can also write:
Const floor = ~ ~ 6.8; / / 6Copy the code
The two-nonbit operator method only works with 32-bit integers. Therefore, for any number higher than that, math.floor () is recommended.
conclusion
One final tip – don’t use arbitrary coding styles. There should be an enforcement standard.
Finish, I am small wisdom, finish finishing, ready to dish LoL, remember to praise attention, make a fortune oh
The bugs that may exist after code deployment cannot be known in real time. In order to solve these bugs, I spent a lot of time on log debugging. Incidentally, I recommend a good BUG monitoring tool for youFundebug.
Original text: dev. To/apoorvtyagi…
communication
Have a dream, have dry goods, wechat search [big Move the world] pay attention to this in the early morning is still in the bowl washing wisdom.
In this paper, making github.com/qq449245884… Has been included, a line of large factory interview complete test sites, information and my series of articles.