Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 practical tips of JavaScript?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you what the 10 practical tips about JavaScript are, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I have been looking for new ways to improve efficiency. JavaScript is always full of surprises.

1. Convert arguments objects to an array

The arguments object is an array-like object accessible within a function that contains the values of the parameters passed to the function.

But unlike other arrays, we can access its element value and get the length, but we cannot use other array methods on it.

Fortunately, we can convert it to a regular array:

Var argArray = Array.prototype.slice.call (arguments)

two。 Sum all the values in the array

My original idea was to use loops, but that would be too much work.

Var numbers = [3,5,7,2]; var sum = numbers.reduce ((x, y) = > x + y); console.log (sum); / / returns 17

3. Conditional short circuit

We have the following code:

If (hungry) {goToFridge ();}

By using variables with functions, we can make them shorter:

Hungry & & goToFridge ()

4. Use logical or for conditions

I used to declare my variables at the beginning of a function, just to avoid getting undefined in the event of any unexpected errors.

Function doSomething (arg1) {arg1arg1 = arg1 | | 32; / / if the variable has not been set, arg1 will take 32 as the default}

5. Comma operator

The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last Operand.

Let x = 1; x = (xylene, x); console.log (x); / / expected output: 2 x = (2,3); console.log (x); / / expected output: 3

6. Resize the array with length

You can resize or empty the array.

Var array = [11Pert 12array 13, 14, 15]; console.log (array.length); / / 5 array.length = 3; console.log (array.length); / / 3 console.log (array); / / [11pr 12array 13] array.length = 0; console.log (array.length); / / 0 console.log (array); / / []

7. Exchange values by array deconstruction

Deconstructing assignment syntax is a JavaScript expression that decompresses values in an array or attributes in an object into different variables.

Let a = 1, b = 2 [a, b] = [b, a] console.log (a) / /-> 2 console.log (b) / /-> 1

8. Randomly arranging elements in an array

I shuffle the cards every day.

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

9. Attribute names can be dynamic

You can assign dynamic properties before declaring the object.

Const dynamic = 'color'; var item = {brand:' Ford', [dynamic]: 'Blue'} console.log (item); / / {brand: "Ford", color: "Blue"}

10. Filter unique valu

For all ES6 enthusiasts, we can create a new array with only unique values by using the Set object with the expansion operator.

Const my_array = [1, 2, 2, 3, 3, 4, 5, 5] const unique_array = [... new Set (my_array)]; console.log (unique_array); / / [1, 2, 3, 4, 5] are the 10 practical tips of JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report