7. JavaScript Operators

The role of this teaching is limited to students who need to get started with 0 basics. The
WeChat public account [Front-end New Weather] often shares front-end knowledge content. You
can join the self-study learning group (add the group in the menu bar at the bottom of the public account), and it is more convenient to learn with peers.

Courseware code address: https://github.com/haojiey/js-Introductory-courseware

JavaScript Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, division, etc.

operator name example
+ addition x + y means to calculate the sum of x plus y
- subtraction x - y means calculate the difference between x minus y
* multiplication x * y means to calculate the product of x times y
/ division x / y means to calculate the quotient of x divided by y
% modulo (remainder) x % y means to calculate the remainder of dividing x by y
++ self-increment ++x adds 1 to x and returns the value of x
x++ returns the value of x, then adds 1 to x
- - Decrement - -x subtract 1 from x and return the value of x
x-- returns the value of x, then decrements x by 1
var x = 10,
var y = 5;
console.log("x + y =", x + y);
console.log("x - y =", x - y); 
console.log("x * y =", x * y);
console.log("x / y =", x / y);
console.log("x % y =", x % y);
let x = 5;
x++;

let y = 5;
y--;

let x = 5;
let y = x++;
let z = ++x; 

JavaScript assignment operator

Assignment operators are used to assign values ​​to JavaScript variables.

operator describe example Equivalent to
= The simplest assignment operator, which assigns the value on the right side of the operator to the variable on the left side of the operator x=y Assign variable x to y
+= Do the addition first, then assign the result to the variable on the left side of the operator x+=y x=x+y
-= Perform the subtraction first, then assign the result to the variable on the left side of the operator x-=y x=x-y
*= Do the multiplication first, then assign the result to the variable on the left side of the operator x*=y x=x*y
/= Perform division first, then assign the result to the variable on the left side of the operator x/=y x=x/y
%= First perform the modulo operation, and then assign the result to the variable on the left side of the operator x%=y x=x%y
var x = 10;
x += 20;
console.log(x); 
var x = 12,
    y = 7;
x -= y;
console.log(x); 
x = 5;
x *= 25;
console.log(x);
x = 50;
x /= 10;
console.log(x); 
x = 100;
x %= 15;
console.log(x);

JavaScript string operators

  • Operators are used to add (concatenate) text values ​​or string variables.
    To concatenate two or more string variables, use the + operator.
var x = "Hello ";
var y = "World!";
var z = x + y;
console.log(z);
x += y;
console.log(x); 

JavaScript comparison operators

The comparison operator is used to compare the expressions on the left and right sides of the operator. The operation result of the comparison operator is a Boolean value, and there are only two results, either true or false.

operator name example
== equal x == y means true if x is equal to y
=== congruent x === y means true if x is equal to y, and x and y are also of the same type
!= not equal x != y means true if x is not equal to y
!== Insufficiency, etc. x !== y means true if x is not equal to y, or x and y are of different types
< less than x < y means true if x is less than y
> more than the x > y means true if x is greater than y
>= greater than or equal to x >= y means true if x is greater than or equal to y
<= less than or equal to x <= y means true if x is less than or equal to y
var x = 25;
var y = 35;
var z = "25";

console.log(x == z); 
console.log(x === z); 
console.log(x != y); 
console.log(x !== z);
console.log(x < y);
console.log(x > y);
console.log(x <= y);
console.log(x >= y);

JavaScript Logical Operators

Logical operators are usually used to combine multiple expressions. The result of a logical operator is a Boolean value, which can only have two results, either true or false. The following table lists the logical operators supported in JavaScript:

operator name example
&& logic and x && y means true if both x and y are true
|| logical or x || y means true if either x or y is true
! logical NOT !x means true if x is not true
var a = true;
var b = true;

if(a && b){
    
    console.log('逻辑与')}
if(a || b){
    
    console.log('逻辑或')}
if(!a){
    
    console.log('逻辑非')}

JavaScript ternary operator

The ternary operator (also known as the conditional operator) consists of a question mark and a colon. The syntax is as follows:
conditional expression? expression1: expression2;
if the result of the "conditional expression" is true (true ), execute the code in "Expression 1", otherwise execute the code in "Expression 2".

var x = 1,
  y = 2;
x > y ? console.log("x 大于 y") : console.log("x 小于 y");

Guess you like

Origin blog.csdn.net/weixin_55123102/article/details/130330013