This is my second post on getting started

preface

Hello again, this time I’m going to talk to you a little bit about my understanding of operators in front end work. Before we start, let me explain what an operator is. Operator – The object to which the operator applies. For example, the multiplication 1*2 has two operators: left operator 1 and right operator 2. The middle asterisk is the operator, and an operator with two operators is a binary operator, so what is a unary operator and what is a ternary operator

Unary operator

++,--The operator

These operators are mostly used inside functions or in for loops. Such as

const arr = [1];
for(let i = 0; i < arr.length; i++) {}
Copy the code

In addition, there is a difference between putting ++,– before and after an operator. Here’s a common interview question:

let variable = 0;
console.log(variable++);
console.log(++variable);
console.log(--variable);
console.log(variable--);
console.log(variable)
Copy the code

The answer is: 0, 2, 1, 1, 0. The ++ operator performs an assignment on its own variable, while the ++ operator performs an assignment on its own variable, and then performs an operation on it.

let variable = 0;
console.log(variable++); // The ++ is behind, so we operate on the original variable first, i.e. Console. The original value was 0.
console.log(++variable); // The ++ is in front, so it will be calculated first. At this point, variable is already 1, and when I do the calculation, it's 2
console.log(--variable); // By the above logic, this is 1
console.log(variable--); // This is still 1
console.log(variable)    // This is 0.
Copy the code

-The minus operator

What it does is it converts numbers between positive and negative

let variable = 1;
let otherVariable = -variable
console.log(otherVariable) // -1
Copy the code

~Bitwise non-operator

What it does is it inverts every binary. So far, I use it when I’m looking for a value in an array in a project.

const variable = 1 const arr = [1, 2, 3] if (arr.indexof (variable) > -1) {console.log(' exist ')} // But since we know the principle of bitwise non-operators, we can abbreviate this by saying,Copy the code

Because if it doesn’t exist, then it returns -1, and then ~-1 is 0, and 0 is false, so it doesn’t go into the if block. Instead, it can go into the if block.

The delete operator.

The delete operator is used to delete a property of an object, such as

let obj = { name: 'nancy'.age: 25 }
const returnValue = delete obj.age
console.log(obj) // { name: 'nancy' }
console.log(returnValue) // true
Copy the code

If you delete an object, it will return true, even if you delete a property that doesn’t exist in the object, it will return true. The interesting thing is, if you delete a property on the object that has a property of the same name, then you access that property after you delete that property on the object, you access that property on the stereotype chain.

function person () { this.name = 'bob' }
person.prototype.name = 'nancy'
var man = new person()
console.log(man.name) // bob
delete man.name
console.log(man.name) // nancy
Copy the code

Binary operator

||and&&The operator

It’s usually used in if statements, but the most common way I use it in projects other than if statements is to use it as a short circuit. Such as

let money = getMoney() || 0
Copy the code

If the getMoney method gets a null value, a similar scenario would be given a default value of 0 && operator, such as in vue

this.$refs.table && this.$refs.table.initData()
Copy the code

We can use ampersand to do a null, so we have one less if, and the code looks a little cleaner

??The operator

It is ES2020 introduced a new operator and | | usage similar at some time. Such as

let name = hello ?? 'hello world'
Copy the code

If the hello is null or undefined, it is the name to the hello world, if we use | | statement, if the hello is an empty string or 0, or false words, also will take to the hello world

Other binary operators

= =and= = =

console.log(0= =false) // true
console.log(0= = =false) // false
Copy the code

%The operator

The most common use of it in projects is to determine parity

let isOdd = num % 2 === 0
Copy the code

The +, -, *, /, +, - =, * = / = % =The addition, subtraction, multiplication, and division operators, there’s not much to say about this.

支那Power operator

console.log(2支那3) / / 8
Copy the code

An operator

To tell you the truth, I am not familiar with the bit operator. When I was looking at the vue-Next source code, I saw that it was used to mark some content when doing some compiler optimization.

Ternary operator

let pi = zuo ? die : live // If you don't do it, you won't die. Ha, ha, ha
Copy the code

conclusion

Well, it’s getting late, so that’s all for today’s review. See you next time ~ 😃