Operators of JavaScript knowledge points

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

one, arithmetic operators and assignment operators

The arithmetic operators provided by JavaScript have +,-, *,/,%. The value after the operation is assigned by =. For x = x operator y, we can also apply the compound assignment operator, which means x + = y equals x = x + y.

+ can also be used as a string connection, as

1 var str = ' Hello ' + ' world '; 2 // Hello World

If you try to add a string to a number (or other type), the other type will be converted to a string first. So please accept the following facts:

var str1 = "3" + 4 + 5//  345var str2 = 3 + 4 + "5"//  75 

Maybe the result will upset you, so if you want to do an addition, make sure two numbers are digits. But it also sometimes brings convenience to us. For example, after the number of strings, you can convert to a string type.

1 var num = 5; 2 var str = 5 + "; 3 alert (typeof//  string4//  5

The division operator in JavaScript is not rounded like some other language, and its result can be decimal.

// 2.5
second, relational operators

The relational operators provided by JavaScript are less than (<), greater than (>), less than or equal (<=), greater than or equal (>=), equal (= =), unequal (! =), congruent (identical) (= = =), not congruent (not identical) (!==).
They can be applied to numbers and strings. for = = =, both the value and the type are equal. for = =, as long as the converted values are equal. This also applies to! = and!==.

// false // true

Please try to use = = = and!== instead of another group, see some strange examples below you will know.

Alert (' = = ' 0 ');//falseAlert (0 = = ");//trueAlert (0 = = ' 0 ')//trueAlertfalse= = ' false ')//falseAlertfalse= = ' 0 ')//trueAlertfalse= = undefined)//falseAlertfalse==NULL)//falseAlertNULL= = undefined);//truealert (undefined = = 0);//falseAlertNULL= = 0);//falseAlert (' \t\r\n ' = = 0);//true

O (∩_∩) o~ Halo! However, if the above operation uses = = =, The returned result is false, because the two operands have different types.

third, logical operators

JavaScript has three logical operators: logical AND (and), logical or (or), logical non (not). Where logic is associated with (&) and logic or (| |) Has a short circuit principle.

that is, for a && B, if A is false, B is not computed, the result is returned directly false; for a | | b, if A is true, B is not evaluated and the result returns true directly. This feature can be convenient for us, such as:

Before accessing the properties of an object, let's determine if the object is empty:
var name = o && o.getname ();
Set Default values:
var name = Othername | | "Default";
Avoid assigning null or undefined values to variables.
var obj = Oneobject | | Twoobject; Assign one of the valid variable values to obj

Four, bitwise operators

There are seven bitwise operators, namely, bit non-not (~), Bit and and (&), bit or OR (|), bitwise XOR (^), left Shift (<<), signed right Shift (>>), unsigned Right shift (>>>).
In C + +, the bitwise operator handles integers. JavaScript does not have integer types, only double-precision floating-point numbers. Therefore, bitwise operators convert their numeric operands to integers first, then perform calculations, and then convert back. In C + +, these bitwise operators are close to hardware processing, so they are very fast. JavaScript's execution environment is generally less exposed to hardware, so it is very slow. JavaScript is rarely used to perform bit operations.

To be Continued ...

Operators of JavaScript knowledge points

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.