Home >Web Front-end >JS Tutorial >【JavaScript Tutorial】JavaScript Operators

【JavaScript Tutorial】JavaScript Operators

黄舟
黄舟Original
2016-12-24 15:05:081070browse

JavaScript Operator

Operator = is used for assignment.

Operator + is used to add value.

Operator = is used to assign values ​​to JavaScript variables.

The arithmetic operator + is used to add values ​​together.

Example

Specify variable values ​​and add the values:

y=5;
z=2;
x=y+z;

After the above statement is executed, the value of Arithmetic operations between values. =""


Operator


Description

Example

x Operation Result

y Operation Result

Online examples

+ Addition x=y+2 7 5 Examples»


- Subtraction x=y-2 3 5 Examples»

* Multiplication x=y* 2 10 5 Example»

/ Division x= y/2 2.5 5 Example»

% Modulo (remainder) x=y%2 1 5 Example»

++ Auto-increment x=++y 6 6 Example»

x=y++ 5 6 Example»

-- Decrement x=--y 4 4 Example»

x=y-- 5 4 Example»

JavaScript assignment operator

The assignment operator is used to assign values ​​to JavaScript variables.

Given x=10 and y=5, the following table explains the assignment operator:

operator


example

is equivalent to

operation result

online example

= x =y x=5 Example»


+= x+=y x=x+y x=15 Example»

-= x-=y x=x-y x=5 Example» *=y x= x*y x=50 Example»

/= x/=y x=x/y x=2 Example»

%= x%=y x=x%y x=0 Example»

for strings The + operator

The + operator is used to add (concatenate) text values ​​or string variables.

If you need to connect two or more string variables, please use the + operator.

Example

If you need to connect two or more string variables, please use the + operator:

7

txt3 The operation result is as follows:

txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;

If you want to add a space between two strings, you need to Insert spaces into a string:

Example

What a verynice day

After the above statement is executed, the value contained in variable txt3 is:

txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;

Or insert spaces into the expression::

Example

What a very nice day

After the above statement is executed , the value contained in the variable txt3 is:

txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;

Add strings and numbers

Add two numbers and return the sum of the added numbers. If a number is added to a string, a string is returned, as shown in the following example:

Example

What a very nice day

x, y, and z output results are:

10
55
Hello5

规则:如果把数字与字符串相加,结果将成为字符串!


 以上就是【JavaScript教程】JavaScript 运算符的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement:
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 admin@php.cn