Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan ยท April More text Challenge”. Click here for more details.


As we all know, JS has a lot of tricks (tricks), although some basic use is rarely, but accidentally encountered, at least to be able to read it ~

Don’t say more words, directly open blunt ~

Trick 1: Two sets of logic and or

Look at the following code snippet:

let a="one",b="two",c="three";

let d = a && b && c; 

console.log(d)
Copy the code

Output what?

Is that true? Or one of one/two/three?

The correct answer is:

// "three"
Copy the code

To:

let a = "",b="two",c="three";

let d = a && b && c; 

console.log(d)
Copy the code

Output what?

Is false? Or one of /two/three?

The answer is:

/ / ""Copy the code

Wait, before you jump to conclusions, let’s look at the “or” operation:

let a="one",b="two",c="three";

let d = a || b || c; 

console.log(d)
Copy the code

Is the output still three?

False, the result is:

 // "one"
Copy the code

Then look at:

let a = "",b="two",c="three"; 

let d = a || b || c;

console.log(d)
Copy the code

The output is:

  // "two"
Copy the code

Oh oh ~ see here, must have a big smart guess the truth ~~

  • AND (&&)

Two sets of logical and (&&) operators represent:

When each of these values is “true”, the last value is taken;

When there is a value of “false”, the first value is “false”;

  • OR (||)

Two groups of logic or (| |) operator said:

When there is a value “true”, the first value is “true”;

When each of these values is false, the last value is taken;

Trick 2: Sets the variable to key

Sometimes you encounter the requirement that the key of an object is a variable; So how do we set that up?

Like this?

var stu_address = 'address';
var student = {
    name:'mick',
    age : 10,
    stu_address : 'chennai'    
    
}
// {name:'mick', age : 10, stu_address : 'chennai'}
Copy the code

Output, did not achieve the desired effect.

“Trick” came on!

var stu_address = 'address';
var student = {
    name:'mick',
    age : 10,
    [stu_address] : 'chennai'    
    
}

//{name: 'mick', age: 10, address: 'chennai'}
Copy the code

If the key in the object is wrapped with [], the key can be set as a dynamic key.

Trick 3: Data and object conversion

This is common, but important and useful

Array to object:

Let arr =,2,3,4,5,6,7,8,9 [1] const convert_obj = {... arr}; / / {" 0 ": 1," 1 ": 2," 2 ": 3," 3 ": 4," 4 ", 5, "5" : 6, "6", 7, "7" : 8, "8" : 9, "9" : 10}Copy the code

Object to array:

let obj = {
            one : 'a',
            two : 'b',
            three : 'c'
        };

/*1. Object.keys*/

         const keys = Object.keys(obj) //['one', 'two', 'three']

/*2. Object.values*/             
           
           const values = Object.values(obj) // ['a', 'b', 'c']

/*3. Object.entries*/

             const entries = Object.entries(obj) // [ ["one", "a"], ["two", "b"], ["three","c"] ]
Copy the code

For this melon, the first trick is still good drop, usually used less, can strengthen understanding;

OK, the above is the share, hope can help ~

Think good, give a three link ๐Ÿ‘๐Ÿ‘๐Ÿ‘

I’m Nuggets Anthony, output exposure input, technical insight into life.