[JavaScript tutorial] JavaScript Operators

Source: Internet
Author: User
[JavaScript tutorial] JavaScript Operators

JavaScript Operators

Operator = is used to assign values.

Operator + is used for value adding.

Operator = is used to assign values to JavaScript variables.

Arithmetic Operator + is used to add up values.

Instance

Specify the variable value and add the value together:

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

After the preceding statement is executed, the value of x is:

7

JavaScript Arithmetic Operators



Operator

Description

Example

X operation result

Y operation result

Online instance


+ Addition x = y + 2 7 5 instances»

-Subtraction x = y-2 3 5 instances»

* Multiplication x = y * 2 10 5 instances»

/Division x = y/2 2.5 5 instances»

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

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

X = y ++ 5 6 instances»

-- Auto-Subtract x = -- y 4 4 instance»

X = y -- 5 4 instances»

JavaScript assignment operator

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

Given x = 10 and y = 5, the following table describes the assignment operators:


Operator

Example

Equivalent

Calculation Result

Online instance


= X = y x = 5 instances»

+ = X + = y x = x + y x = 15 instances»

-= X-= y x = x-y x = 5 instances»

* = X * = y x = x * y x = 50 instances»

/= X/= y x = x/y x = 2 instance»

% = X % = y x = x % y x = 0 instance»

+ Operator used for strings

+ Operators are used to add up (join) text values or string variables ).

To connect two or more string variables, use the + operator.

Instance

To connect two or more string variables, use the + operator:

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

The calculation result of txt3 is as follows:

What a verynice day

To add spaces between two strings, you need to insert spaces into a string:

Instance

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

After the preceding statement is executed, the txt3 variable contains the following values:

What a very nice day

Or insert spaces into the expression ::

Instance

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

After the preceding statement is executed, the txt3 variable contains the following values:

What a very nice day

Addition of strings and numbers

The sum of the two numbers is returned. If the number and string are added, the string is returned, as shown in the following example:

Instance

x=5+5;y="5"+5;z="Hello"+5;

The output results of x, y, and z are:

1055Hello5

Rule: if you add a number to a string, the result is a string!


The above is the content of the [JavaScript tutorial] JavaScript operator. For more information, see the PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.