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

Which JavaScript code can be operated quickly?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Which JavaScript code can be operated quickly, I believe many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

JavaScript is a magical language, some of its features are elusive, but its simplicity and flexibility are also admirable. Some functional logic may require a lot of code in a conventional way, but with some API and syntax features, it can be done with just one line of code!

1. Get random Boolean value (true/false)

Math.random () returns a random number between 0 and 1, so you can use whether the return value is less than 0.5 to return a random Boolean value.

Const randomBoolean = () = > Math.random () > = 0.5; console.log (randomBoolean ())

two。 Reverse string

Combined with the inversion method of the array, you can reverse the string:

Const reverse = str = > str.split (''). Reverse (). Join (''); reverse ('hello world'); / / Result:' dlrow olleh'

3. Array deduplication

Frequently asked questions in the interview, the lazy way is to use Set.

Let removeDuplicates = arr = > [... new Set (arr)]; console.log (removeDuplicates (['foo',' bar', 'bar',' foo', 'bar'])); / / [' foo', 'bar']

4. Determine whether the browser Tab window is active

The document.hidden property can be used to determine whether the browser window is visible (the currently active window).

Const isBrowserTabInView = () > document.hidden; isBrowserTabInView ()

5. Judge the parity of numbers

For primary school math problems, just use% 2 to judge:

Const isEven = num = > num% 2 = 0; console.log (isEven (2)); / / Result: true console.log (isEven (3)); / / Result: false

6. Get the time part of the date object

The .toTimeString () method of the date object can get a string in time format, and just intercept the previous part:

Const timeFromDate = date = > date.toTimeString (). Slice (0,8); console.log (timeFromDate (new Date (2021, 0,10,17,30,0)); / / Result: "17:30:00" console.log (timeFromDate (new Date (); / / Result: will log the current time

7. Digits truncated decimal places

If you need to truncate the decimal places of a floating point number (not rounded), you can do so with Math.pow ():

Const toFixed = (n, fixed) = > ~ (Math.pow (10, fixed) * n) / Math.pow (10, fixed); / / Examples toFixed (25.198726354, 1); / / 25.1 toFixed (25.198726354, 2); / / 25.19 toFixed (25.198726354, 3); / 25.198 toFixed (25.198726354, 4); / / 25.1987 toFixed (25.198726354, 5) / / 25.19872 toFixed (25.198726354, 6); / / 25.198726

8. Determine whether the DOM element has gained focus

Const elementIsInFocus = (el) = > (el = document.activeElement); elementIsInFocus (anyElement)

9. Determine whether the current environment supports touch events

Const touchSupported = () = > {('ontouchstart' in window | | window.DocumentTouch & & document instanceof window.DocumentTouch);} console.log (touchSupported ())

10. Determine whether it is an Apple device

Const isAppleDevice = / Mac | iPod | iPhone | iPad/.test (navigator.platform); console.log (isAppleDevice)

11. Scroll to the top of the page

The window.scrollTo () method accepts x and y coordinate parameters, which are used to specify the scrolling target location. All set to 0, you can go back to the top of the page. Note: the .scrollto () method is not supported by IE.

Const goToTop = () = > window.scrollTo (0,0); goToTop ()

twelve。 Find the average value

A typical application scenario of reduce: array summation.

Const average = (... args) = > args.reduce ((a, b) = > a + b) / args.length; average (1,2,3,4); / / Result: 2.5 after reading the above, have you mastered any JavaScript code that can be operated quickly? 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!

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