In the process of project development, I always feel that there is a better way to organize code, through code snippets, indirectly learn some code organization skills. Reading open source code is the fastest way to broaden your coding mind. If you think it will help, check out the column “Learning JavaScript Code in Fragments” or leave a comment in the comments section.

Deconstruction alias

It’s easy to have an object and want to create a variable from one of its properties, but often you want to use a variable name that is different from the property name. For example, if you have the title property of a blog detail and want it to be the value of the page’s title, pageTitle, the following code snippet shows a more concise way of deconstructing the alias:

Const detail = {title: "JavaScript coding skills ", intro: "Reading open source code is the fastest way to open your mind ",}; const { title: pageTitle } = detail; console.log(pageTitle); // JavaScript coding skillsCopy the code

Shuffle the deck

In the era of big data, algorithm is a very common knowledge, usually many algorithms need to transform array elements. In an interview, you might be asked how to make a game. Most games involve some sort of shuffling algorithm.

const arrayNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; Const shuffled = (array) => array.sort() => {return math.random () -0.5; }); console.log(shuffled(arrayNumbers));Copy the code

Type conversion

The flexibility of JavaScript also leads to multiple implementations of converting one type to another in JavaScript. Some look more concise, so make sure you don’t reduce the readability of your code by using it in a particular way.

To a String

const num = 5; const str1 = num.toString(); const str2 = String(num); const str3 = num + ""; const str4 = `${num}`; console.log(str1); // "5" console.log(str2); // "5" console.log(str3); // "5" console.log(str4); / / "5"Copy the code

To a Number

const str = "1"; const num1 = +str; const num2 = Number(str); console.log(num1); // 1 console.log(num2); / / 1Copy the code

Floating point to integer

As we all know, the number conversion in JavaScript is special, especially when it comes to math operations. JavaScript is prone to problems that do not occur in other programming languages, such as 0.1+0.2! =0.3, the following code snippet shows how common floating point numbers can be converted to integers:

Const floatNum = 1.846; const int1 = floatNum | 0; const int2 = parseInt(floatNum); const int3 = ~~floatNum; const int4 = Math.round(floatNum); const float1 = Math.floor(floatNum); const float2 = Math.ceil(floatNum); console.log(int1); // 1 console.log(int2); // 1 console.log(int3); // 1 console.log(int4); // 2 console.log(float1); // 1 console.log(float2); / / 2Copy the code

Here’s how to round the decimal point:

Const floatNum = 1.855; const roundNumber = (num, precision = 2) => { const numberFormatted = new Intl.NumberFormat("en-US", { maximumFractionDigits: precision, }).format(num); return Number(numberFormatted); }; const num1 = floatNum.toFixed(2); const num2 = Number(floatNum.toFixed(2)); const num3 = Math.round(floatNum * 100) / 100; console.log(num1); / / 1.85 console. The log (num2); / / 1.85 console. The log (num3); / / 1.86 console. The log (roundNumber (1.2549)); / / 1.25 console. The log (roundNumber (1.255)); / / 1.26Copy the code

Converted to Boolean

const str = "1"; const bool1 = !! str; const bool2 = Boolean(str); console.log(bool1); // true console.log(bool2); // trueCopy the code

A positive integer can be converted to two, eight, or hexadecimal

const int = 10; const binary = int.toString(2); const hex = int.toString(16); const octal = int.toString(8); console.log(binary); // 1010 console.log(hex); // a console.log(octal); / / 12Copy the code