Discuss some common JavaScript operators and their usage

PHPz
Release: 2023-04-25 11:38:37
Original
459 people have browsed it

In JavaScript, an operator is a special symbol used to handle operations on data types such as numbers, strings, and Boolean values. This article will discuss some common JavaScript operators and their usage.

  1. Arithmetic operators

Arithmetic operators include addition, subtraction, multiplication, division, remainder, etc. For example:

var a = 10;
var b = 3;
var c = a + b; // 加,结果为13
var d = a - b; // 减,结果为7
var e = a * b; // 乘,结果为30
var f = a / b; // 除,结果为3.3333333333333335
var g = a % b; // 求余,结果为1
Copy after login
  1. Assignment operator

The assignment operator is used to assign a value to a variable. For example:

var a = 10;
var b = a; // 将a的值赋给b
Copy after login

In addition, there are some combination assignment operators, such as:

var a = 10;
a += 5; // 等价于a = a + 5,结果为15
a -= 3; // 等价于a = a - 3,结果为12
a *= 2; // 等价于a = a * 2,结果为24
a /= 4; // 等价于a = a / 4,结果为6
a %= 2; // 等价于a = a % 2,结果为0
Copy after login
  1. Comparison operator

Comparison operator is used to compare two The magnitude or equality of values. For example:

var a = 10;
var b = 5;
var c = (a > b); // 大于,结果为true
var d = (a < b); // 小于,结果为false
var e = (a >= b); // 大于等于,结果为true
var f = (a <= b); // 小于等于,结果为false
var g = (a == b); // 相等,结果为false
var h = (a != b); // 不相等,结果为true
Copy after login

Note that == is used to compare the equality of values, not the equality of types. For example, the following expression evaluates to true:

var x = 10;
var y = "10";
var z = (x == y); // 结果为true
Copy after login
  1. Logical Operators

Logical operators are used to process Boolean type values. For example:

var a = true;
var b = false;
var c = !a; // 非,结果为false
var d = a && b; // 与,结果为false
var e = a || b; // 或,结果为true
Copy after login

Among them, ! means negation, && means logical AND, and returns true only when both values ​​are true, otherwise it returns false; || means logical OR, as long as one of the values ​​is true. It returns true, otherwise it returns false.

  1. Ternary Operator

The ternary operator is the only operator in JavaScript that has three operands. It can be used for simple conditional judgment. For example:

var a = 10;
var b = (a > 5) ? "大于5" : "小于等于5"; // 如果a大于5,则返回"大于5",否则返回"小于等于5"
Copy after login
  1. String operators

String operators are used to concatenate strings. For example:

var a = "Hello";
var b = "JavaScript";
var c = a + " " + b; // 将a和b连接起来,结果为"Hello JavaScript"
Copy after login

Note that the operator can be used not only for adding numbers, but also for string concatenation.

  1. Increment and decrement operators

The increment and decrement operators can be used to increase or decrease the value of a variable. For example:

var a = 10;
a++; // 自增,结果为11
a--; // 自减,结果为10
Copy after login

You can also place the increment and decrement operators before or after the variable, which will affect the execution order of the operators. For example:

var a = 10;
var b = a++; // 先将a的值赋给b,再自增a,结果为b=10、a=11
var c = ++a; // 先自增a,再将a的值赋给c,结果为c=12、a=12
Copy after login

Summary

This article introduces some common operators and their usage in JavaScript, including arithmetic operators, assignment operators, comparison operators, logical operators, and ternary operations operators, string operators and increment and decrement operators. An in-depth understanding of the usage of these operators will help us better master JavaScript programming skills.

The above is the detailed content of Discuss some common JavaScript operators and their usage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!