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 four hidden useful JavaScript techniques?

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

Share

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

This article will explain in detail how the four hidden useful JavaScript skills are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Here are four little-known features about JavaScript that are useful in certain situations.

+ operator

What? + operator? I've known it since the first grade.

Yes, of course, but it has nothing to do with arithmetic operators:

Const four = 2 + 2

Another use can be seen as "converting a given expression to Number", but it may also reveal other interesting use cases.

Even if you can get these results using the Number () constructor, in some cases, using the + operator seems cleaner.

For the last example, we will override the valueOf () method of the random object, and the + operator "gets" its value by converting its value to Number.

Debug statement

There are many ways to debug JavaScript applications. ️

You can customize most logging statements by the number of different methods connected to the console object.

But I found that a completely different but unknown approach is to use the debugger statement.

For browser-based JS applications, it provides a quick and easy way to place breakpoints and quickly acquire powerful developer tools.

As I said, this may not be the best way to handle some application debugging, but I think it's interesting to demonstrate this "hidden" feature.

You can also enable it for your node script by passing the-inspect flag to connect to your session remotely. However, I suggest attaching the NodeJS application to your IDE debugger to make it cleaner.

Comma operator

I'm not talking about using comma delimiters to separate objects or elements in containers, such as const arr = [2, 7, 4, 8, 8].

In the expression const a = (2p3), the comma operator allows the entire expression in parentheses to be evaluated, so let's look at some interesting examples:

Basically, this operator executes and & & and | | between the left and right operands at the same time, because it executes the state of both operands.

If you are still confused about its use, think of it this way: it allows you to evaluate multiple expressions where JavaScript expects one expression. This may be to present more elegant code, or to make it easier to debug.

Navigator and Performance API

In fact, this is a bit special because these are the two Web API available in the browser.

Web API is usually used with JavaScript, although this is not always the case.

I would like to introduce two Web API that I have widely used in the JavaScript project:

These are just two of the many Web API you can find on the Internet!

On the four hidden useful JavaScript skills are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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