Share some uncommon but useful JS techniques

Share some uncommon but useful JS techniques

Preface

Programming languages ​​usually contain various hidden tricks, and proficient use of these tricks can improve development efficiency. JavaScript is a highly technical language. Mastering common grammatical techniques can not only deepen your understanding of language features, but also simplify code and improve coding efficiency.

Here is a list of some useful JavaScript tips that I believe will be helpful to you one day.

1. Array deduplication

const numbers = [1, 2, 3, 4, 4, 1]
console.log([...new Set(numbers)]) // [1, 2, 3, 4]

2. Filter all false values ​​from the array

const myArray = [1, undefined, null, 2, NaN, true, false, 3]
console.log(myArray.filter(Boolean)) // [1, 2, true, 3]

3. Convert a string to a number

const str = '123'
const num = +str
console.log(typeof num) // number

4. Convert numbers to strings

const num = 123;
console.log(num + ''); // '123'

5. Use the && symbol to abbreviate conditional statements

// Normal writing if (condition) {
    doSomething()
}

// Shorthand for condition && doSomething()

6. console.table() prints a table in a specific format

// [] refers to optional parameters\
console.table(data [, columns]);

parameter:

  • data indicates the data to be displayed. Must be an array or object.
  • columns represents an array containing the names of the columns.

Examples:

function Goods(name, price) {
    this.name = name
    this.price = price
}

const book = new Goods("《Webpack Getting Started to Abandonment》", "¥ 9.00")
const knowledge = new Goods("《Front-end Self-cultivation》", "¥ 99.00")
const ebook = new Goods("node.js course", "¥ 199.00")

console.table([book, knowledge, ebook], ["name", "price"])

Print results:

7. If you want to add an event listener and run it only once, you can use the once option:

element.addEventListener('click', () => console.log('I run only once'), {
    once: true
});

8. To improve the readability of numbers, you can use underscores as separators:

const num = 2_000_000_000
console.log(num) // 2000000000

9. Use the dataset attribute to access the element's custom data attributes (data-*):

<div id="card" data-name="FE" data-number="5" data-label="listCard">
    Card information</div>

<script>
    const el = document.getElementById('card')

    console.log(el.dataset)
    // { name: "FE", number: "5", label: "listCard" }

      console.log(el.dataset.name) // "FE"
    console.log(el.dataset.number) // "5"
    console.log(el.dataset.label) // "listCard"
</script>

Summarize

This is the end of this article about practical JS skills. For more relevant practical JS skills, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS 4 super practical tips to improve development efficiency
  • Four practical tips for JavaScript string operations
  • Practical Javascript debugging skills sharing (summary)
  • Summary of some practical tips in JavaScript
  • JavaScript Practical Code Tips
  • Summary of practical tips in vue.js projects
  • 23 practical tips to improve JavaScript execution efficiency
  • AngularJS Practical Development Skills (Recommended)
  • Forty-nine JavaScript tips and tricks

<<:  Web Design Tutorial (3): Design Steps and Thinking

>>:  How to obtain root permissions in a docker container

Recommend

Is mysql a relational database?

MySQL is a relational database management system....

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

Pure CSS to change the color of the picture

The css technique for changing the color of an im...

JavaScript to show and hide the drop-down menu

This article shares the specific code for JavaScr...

An example of elegant writing of judgment in JavaScript

Table of contents Preface 1. Monadic Judgment 1.1...

Teach you the detailed process of installing DOClever with Docker Compose

Table of contents 1. What is Docker Compose and h...

Chrome plugin (extension) development guide (complete demo)

Table of contents Written in front Preface What i...

Example code for achieving hollowing effect with pure CSS

I have recently studied the hollowing effect. bac...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

Example code of vue custom component to implement v-model two-way binding data

In the project, you will encounter custom public ...

Elementui exports data to xlsx and excel tables

Recently, I learned about the Vue project and cam...

Detailed explanation of MySql automatic truncation example

Detailed explanation of MySql automatic truncatio...