js the operator.

The operator js


js the operators and the operators is substantially the same Java

Arithmetic operators: +, -, *, /,%, +, -

Assignment operator: =, + =, - =, * =, / =,% =

Comparison operators: == = = = =, ==,>,> =, <, <=!!

Bitwise operators: &, |

Logical operators: &&, ||

Pre logical operators:! (Not)

Ternary operator:?:

Other operators: typeof, delete

js the comparison operators == (! or =) and === (! == or) the difference between:

(1) is == equality operator, compare two values ​​are equal, if the two compared values ​​are the same type, direct comparison;

If the two compared values ​​are not the same type, it is automatically converted to the same type and then compare for equality

(2) === strict equality operator, comparing the two values ​​must be the same type, or directly returns false.

E.g:

1 == "1" // true then be converted to the same type of comparison for equality

1 == true // true then be converted to the same type of comparison for equality

1 == (4-3) //true

1 === "1" // false is not the same type, direct return false

1 === "true" // false is not the same type, direct return false

1 === (4-3) // true of the same type, and the values ​​are equal

Operators typeOf

typeOf is a unary operator, in a previous operand, the operand may be of any type.

Its return value is a string that represents the number of this type of operation, such as:

var a = 100; typeOf a; // "number" (and further comprising any number NaN)

var b = "abc"; typeOf b; // "string" (including any string)

var c = true; typeOf c; //"boolean"

var d = undefined; typeOf d; //"undefined"

var d = null; typeOf d; //"object"

TypeOf can keep parentheses behind, when seeking a type of expression can be enclosed in parentheses, for example:

typeOf(1+"100");

This allows typeOf looks like a function, note that it is an operator, not a function!

附: typeof null // "object"

The above codes indicate a null query type, the result is object (object).

This is not to say that null data type is an object, but a convention JavaScript early deployment, in fact, not entirely correct,

Later, it was too late to think of change, will break existing code, has been preserved.

delete operator

delete is a unary operator, which is used to delete the object attributes or elements of the array returned value is a Boolean value indicating whether such successfully deleted.:

var a = [1,2,3]; // define an array

delete a [2]; // remove the last element

alert (a [3]); // last element in the array does not exist

a.length; // => 3 Note that the length of the array does not change, even though the elements have been removed, but the delete operation left a "hole"

It did not affect the length of the array, so the array length is still 3

------------------------------------------------------

Data type conversion in js

Automatically converting data types of the type in js when required, converting the following rules:

Value type:

Transfer string type, directly converted to the corresponding string values, 3 -> "3"

Turn Boolean type, NaN 0 and turn into false, true turn into other values

When required, it is automatically transferred to the corresponding object to be wrapped 100 -> new Number (100)

String:

Empty string ( ""): a value of 0 turn, turn the Boolean value of false

Pure non-empty string values ​​( "123"): the value of the corresponding turn value turn boolean value is true

Non-numerical non-empty string ( "abc"): the value transfer is NaN, the Boolean value of true revolution

When required, is automatically transferred to the corresponding object to be wrapped "aaa" -.> New String ( "aaa")

Boolean:

true: the value of a turn, turn the string is "true"

false: the value is 0 turn, turn the string to "false"

When required, it is automatically transferred to the corresponding object to be wrapped.

undefined

Transfer value is NaN, turn the string is "undefined", turn the Boolean value is false, the object will turn throw an exception!

null

Transfer value is 0, turn string is "null", turn the Boolean value is false, the object will turn throw an exception!

Product is slightly Library http://www.pinlue.com

Guess you like

Origin blog.51cto.com/14325182/2412265