Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the four hidden useful JavaScript techniques?

Shulou Source: shulou.com Published: 2022-06-03 08:11:18 10月01日 Update

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.

Tags: Operators operations methods expressions applications useful interesting two functions objects applications operands programs statements commas techniques different clean content at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Redmi MariaDB Shulou Tech Info