Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the 9 useful JavaScript tips?

Shulou Source: shulou.com Published: 2022-06-03 08:27:52 10月04日 Update

What are the 9 useful JavaScript tips? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The hacker's methodology is a build method that involves continuous improvement and iteration. Hackers think that one day it will get better, and nothing can never be achieved.

Real hackers always solve problems that no one cares about in different ways.

Here are some very powerful JavaScript tips.

1. Replace all

We all know that the string.Replace () function can only replace the first match.

You can replace everything that appears by adding / g to the end of the regular expression.

Var example = "potato potato"; console.log (example.replace (/ pot/, "tom")); / / "tomato potato" console.log (example.replace (/ pot/g, "tom")); / / "tomato tomato"

two。 Extract unique valu

By using the Set object and... Operator can create a new array with unique values.

Var entries = [1, 2, 2, 3, 4, 5, 6, 6, 7, 7, 8, 4, 2, 1] var unique_entries = [... new Set (entries)]; console.log (unique_entries); / / [1,2,3,4,5,6,7,8]

3. Convert a number to a string

All you need is a + operator and an empty string.

Var converted_number = 5 + "; console.log (converted_number); / / 5 console.log (typeof converted_number); / / string

4. Convert a string to a number

You only need to use the + operator. Note, however, that it only applies to "string numbers".

The_string = "123"; console.log (+ the_string); / / 123the_string =" hello "; console.log (+ the_string); / / NaN

5. Randomly arranging elements in an array

This is the best way to shuffle cards:

Var my_list = [1,2,3,4,5,6,7,8,9]; console.log (my_list.sort (function () {return Math.random ()-0.5})); / / [4,8,2,9,1,3,6,5,7]

6. Flattened multidimensional array

Just use. Operator.

Var entries = [1, [2, 5], [6, 7], 9]; var flat_entries = [] .concat (....); / / [1, 2, 5, 6, 7, 9]

7. Conditional short circuit

You only need to give an example to understand:

If (available) {addToCart ();}

Simplify the code by simply using variables and functions:

Available & & addToCart ()

8. Dynamic attribute name

For a long time, I thought I had to declare an object before I could assign dynamic properties, but.

Const dynamic = 'flavour'; var item = {name:' Coke', [dynamic]: 'Cherry'} console.log (item); / / {name: "Coke", flavour: "Cherry"}

9. Adjust the emptying array with length

To resize the array:

Var entries = [1,2,3,4,5,6,7]; console.log (entries.length); / / 7 entries.length = 4; console.log (entries.length); / / 4 console.log (entries); / / [1,2,3,4]

To empty the array:

Var entries = [1, 2, 3, 4, 5, 6, 7]; console.log (entries.length); / / 7 entries.length = 0; console.log (entries.length); / / 0 console.log (entries); / / [] have you mastered 9 useful JavaScript tips after reading the above? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Arrays methods operators operations techniques content characters strings numbers problems hackers ease of use functions dynamic objects properties more adjustments differences powerful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Huawei Shulou Information Shulou Technology