JS Operator Summary

Source: Internet
Author: User
Tags arithmetic operators

Arithmetic operators
Addition operator (+), subtraction operator (-), Division operator (/), multiplication operator (*)
You can also combine a variety of operations: 1+4*5
To avoid ambiguity, separate operations can be separated by parentheses: 1+ (4*5); (1+4)
Variables can contain actions:

var total = (1+4);

You can also manipulate variables:

var = Temp_fahrenheit; var temp_celsius = (temp_fahrenheit-32)/1.8;

JS provides some very useful operators as abbreviations for various common operations. For example, to add 1 to a numeric variable,
You can use the following statement:

Year = year +1;

You have also used the + + operator to achieve the same goal:

year++;

--the operator can also subtract 1 from a numeric variable.
The plus sign (+) is a special operator that can be used either for numeric values or for strings. Put multiple strings
The end-to-end operation is called splicing (concatenation).

var message = "I am Feeling" + "happy";

This stitching can also be done using variables:

var mood = "Happy"; var message = "I am Feeling" + mood;

It's even possible to stitch together values and strings. Because JS is a weakly typed language, this operation is allowed.
At this point, the value is automatically converted to a string:

var year =; var message = "The year was" + year;

Another very useful shortcut operator is + =, which can complete "addition and assignment" (or "stitching and assigning") at once
Operation:

var year =; var message = "The year is"+ = year;alert (message);

The output was the year is 2010

Comparison operators:
To compare two values for equality, you can use the Equals comparison operator. (==);
A single equal sign (=) is used to complete the assignment.
Example:

var my_mood = "Happy"; var your_mood = "sad"; if (My_mood = your_mood) {alert ("We both feel the same.") );}

The above statement does not complete the comparison manipulation, but only assigns the Your_mood to the variable My_mood.
The correct statement should use the comparison operator (= =);

var my_mood = "Happy"; var your_mood = "sad"; if (My_mood = = Your_mood) {alert ("We both feel the same.") );}

JS can also use the "not equal to" comparison operator (! =).

if (My_mood! = your_mood) {alert ("We ' re feeling different moods.") );}

And also! The equality operator (= =) does not mean strict equality (= = =), compares a false with an empty string with the equality operator (= =)
The result is the same, because the equality operator (= =) considers the empty string to be the same as false. To make a rigorous comparison, you need to
Use strict equality (= = =); This equality operator performs a strict comparison, not only comparing values, but also comparing the types of variables:

var false ; var b = ""; if (A = = b) {alert ("a equals B");}

The equality operator considers false to be not the same type as an empty string. This is true for the unequal operator! =. Want to compare strictly unequal
It is necessary to use!===.

Logical operators:
The operand of the logical operator is a Boolean value. Each logical operand returns a Boolean value of TRUE or false.

The "Logical and" operator, consisting of two "&" characters (&&), is a logical operator. "Logic and"
The operation is true only if its two operands are true.

if (NUM >= 5 && num <= ) {alert ("The number is in the right range.") );}

The logical OR operator is comprised of two vertical straight line characters (| |). As long as one of its operands is true,
The logical OR operation will be true. If its two operands are true, the "logical OR" operation Also
Will be true. The logical OR operation is false only if its two operands are false.

if (Num > | | num < 5 ) {alert ("The number is in the right range.") );}

The "logical Non" operator, which consists of an exclamation point (! ) are composed individually. The "logical non" operator can only be used for
The result of a single logical operand is to reverse the Boolean value returned by that logical operand. If that logic
The Boolean value returned by Count is true, and the "logical non" operator takes it back to false:

if ( ! (1>2)) {Alert ("All are well and the World");}

You can use the "logical non" operator to invert the results of the entire conditional statement.
The following example uses a pair of parentheses to ensure that the logical non operator will act on the combined result of two logical operands:

if ( ! (num>10 | | num<5)) {Alert ("The number is in the right range.") );}

JS Operator Summary

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.