In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "what are the skills to make the code simpler in JS development". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "what are the skills to make the code simpler in JS development" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.
1 Convert Boolean
In addition to the regular Boolean values true and false, JavaScript treats all other values as 'truthy' or 'falsy'.
Unless otherwise defined, all values in JavaScript are 'truthy', except 0,"", null, undefined, NaN, and of course false, which are all'falsy'
We can easily switch between true and false by using the negative operator. It also converts the type to "boolean."
const isTrue = ! 0;const isFalse = ! 1;const alsoFalse = !! 0; console.log (isTrue); // Result: trueconsole.log (typeof true); // Result: "boolean" 2Convert numbers
Using the addition operator + quickly achieves the opposite effect.
let int = "15";int = +int;console.log(int); // Result: 15console.log(typeof int); Result: "number"
This can also be used to convert Boolean values to numbers as follows
console.log(+true); // Return: 1 console.log(+false); // Return: 0
In some contexts,+ will be interpreted as a concatenation operator rather than an addition operator. When this happens (you want to return an integer, not a floating point), you can use two tilde signs:~~.
Consecutive use of two waves effectively negates the operation because-(-n-1)-1 = n + 1 - 1 = n. In other words,~-16 equals 15.
const int = ~~"15"console.log(int); // Result: 15console.log(typeof int); Result: "number"
Although I can't think of many use cases, the bitwise NOT operator can also be used on Boolean values: ~true = \-2 and ~false = \-1.
3 Convert string
To quickly convert numbers to strings, we can use the concatenation operator + followed by a set of empty quotes "".
const val = 1 + "";console.log(val); // Result: "1"console.log(typeof val); // Result: "string"4 Floating point number to integer
If you want to convert floating point numbers to integers, you can use Math.floor(), Math. ceiling (), or Math.round(). But there's a faster way to use it.|(bitwise or operator) truncates a floating-point number to an integer.
console.log(23.9 | 0); // Result: 23console.log(-23.9 | 0); // Result: -23
|The behavior of depends on whether you are dealing with positive or negative numbers, so it is best to use this shortcut only in certain cases.
If n is positive, then n| 0 effectively rounds down. If n is negative, it is effectively rounded up. More precisely, this operation removes anything after the decimal point, truncating floating-point numbers to integers.
You can use ~~ to get the same rounding effect, and as mentioned above, virtually any bit operator forces a floating point number to be an integer. These special operations work because once forced to an integer, the value remains unchanged.
Delete the last digit
The bitwise OR operator can also be used to delete any number of digits from the end of an integer. This means we don't need to use such code to convert between types.
let str = "1553";Number(str.substring(0, str.length - 1)); Conversely, the bitwise OR operator can be written as: console.log (1553 / 10 | 0) // Result: 155console.log(1553 / 100 | 0) // Result: 15console.log(1553 / 1000 |0) // Result: 15 Format JSON
Finally, you may have used JSON.stringify before, but did you realize that it also helps you indent JSON?
The stringify() method takes two optional parameters: a displacer function that filters the displayed JSON and a space value.
console.log(JSON.stringify({ alpha: 'A', beta: 'B' }, null, '\t'));// Result:// '{// "alpha": A,// "beta": B// }'6 takes the last item in the array
The array method slice() can accept negative integers, and if provided, it will accept the value at the end of the array, not the value at the beginning of the array.
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];console.log(array.slice(-1)); // Result: [9]console.log(array.slice(-2)); // Result: [8, 9]console.log(array.slice(-3)); // Result: [7, 8, 9]7es6 de-duplication
Set object type is introduced in ES6, with expansion operation... Together, we can use it to create a new array that has only unique values.
const array = [1, 1, 2, 3, 5, 5, 1]const uniqueArray = [... new Set(array)];console.log(uniqueArray); // Result: [1, 2, 3, 5]
Before ES6, isolation unique values would involve much more code than that.
This technique works with arrays containing basic types: undefined, null, boolean, string, and number. (If you have an array containing objects, functions, or other arrays, you need a different method!)
8 More elegant operations
Starting with ES7, you can use the exponent operator ** as shorthand for power, which is faster than writing Math.pow(2, 3). It's a simple thing, but it's on the list because not many tutorials have updated this operator.
console.log(2 ** 3); // Result: 8
This should not be confused with the ^sign commonly used to represent exponents, but in JavaScript it is the bitwise XOR operator.
Before ES7, shorthand existed only for powers of base 2, using the bitwise left shift operator
Read here, this article "JS development to make the code more concise skills" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.