JavaScript operator and the data type

JS arithmetic

Arithmetic operators

Operators description
+ addition
- Subtraction
* multiplication
** Power ( ES2016 )
/ division
% Coefficient (Remainder)
++ Increment
-- Decreasing

Assignment Operators

Operators example Equivalent to
= x = y x = y
+= x + = y x = x + y
-= x - = y x = x - and
*= x * = y x = x * y
/= x / = y x = x / y
%= x% = y x = x% y

Comparison Operators

Operators description
== equal
=== And other equivalent type
!= not equal
!== Does not equal or unequal type
> more than the
< Less than
>= greater than or equal to
<= less than or equal to
? Ternary operator

Logical Operators

&& Logic and
|| Logical or
! Logical NOT

Type Operators

Operators description
typeof The return type of the variable.
instanceof Returns true, if the object is an instance of an object type.

Bitwise Operators
Bitwise operators handling 32 bits.
Any numerical value, the calculation of the operands are converted to 32-bit number. The result is converted back to JavaScript numbers.

Operators description example Equivalent to result Decimal
& versus 5 & 1 0101 & 0001 0001 1
| or 5 | 1 0101 | 0001 0101 5
~ non- ~ 5 ~0101 1010 10
^ XOR 5 ^ 1 0101 ^ 0001 0100 4
<< Zero-padding-left shift 5 << 1 0101 << 1 1010 10
>> Signed right shift 5 >> 1 0101 >> 1 0010 2
>>> Zero fill right shift 5 >>> 1 0101 >>> 1 0010 2

priority

value Operators description Examples
20 ( ) Expressions grouping (3 + 4)
19 . member person.name
19 [] member person["name"]
19 () Function call myFunction()
19 new create new Date()
17 ++ Postfix increment i++
17 -- Decreasing suffix i--
16 ++ Prefix increment ++i
16 -- Prefix decrement --i
16 ! No logic ! (X == y)
16 typeof Types of typeof x
15 ** Exponentiation (ES7) 10 ** 2
14 * Multiply 10 * 5
14 / except 10 / 5
14 % Modulus divider 10 % 5
13 + plus 10 + 5
13 - Less 10 - 5
12 << Left shift x << 2
12 >> Right shift x >> 2
12 >>> Right shift (unsigned) x >>> 2
11 < Less than x < y
11 <= less than or equal to x <= y
11 > more than the x > y
11 >= greater than or equal to x >= y
11 in Object attributes "PI" in Math
11 instanceof Instance of an object instanceof Array
10 == equal x == y
10 === Strictly equal x === and
10 != not equal x! = y
10 !== Strict not equal x! == y
9 & Bitwise AND x & y
8 ^ Bitwise XOR x ^ y
7 | Bitwise or x | Y
6 && Logic and x && y
5 || No logic x || Y
4 ? : condition ? "Yes" : "No"
3 = Assignment x = y
3 += Assignment x + = y
3 -= Assignment x - = y
3 *= Assignment x * = y
3 %= Assignment x% = y
3 <<= Assignment x <<= y
3 >>= Assignment x >>= y
3 >>>= Assignment x >>>= y
3 &= Assignment x &= y
3 ^= Assignment x ^ = y
3 |= Assignment x | = y
2 yield Pause function yield x
1 , comma 7 , 8


JS data types

JavaScript variables capable of storing various data types : numeric, string, array, objects, etc.

var length = 7;                             // 数字
var lastName = "Gates";                    // 字符串
var cars = ["Porsche", "Volvo", "BMW"];         // 数组
var x = {firstName:"Bill", lastName:"Gates"};    // 对象 


JavaScript has a dynamic type. This means that the same variables can be used in different types:

var x;               // 现在 x 是 undefined
var x = 7;           // 现在 x 是数值
var x = "Bill";      // 现在 x 是字符串值


Boolean value that only two values: true or false.

JavaScript array written in square brackets.
Item in the array separated by commas.

var cars = ["Porsche", "Volvo", "BMW"];


JavaScript 对象用花括号来书写。
对象属性是 name:value 对,由逗号分隔。

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};


您可使用 JavaScript 的 typeof 来确定 JavaScript 变量的类型
typeof 运算符返回变量或表达式的类型:
typeof 运算符对数组返回 "object",因为在 JavaScript 中数组属于对象。

typeof ""                  // 返回 "string"
typeof "Bill"              // 返回 "string"
typeof "Bill Gates"          // 返回 "string"
typeof 0                   // 返回 "number"
typeof 314                 // 返回 "number"
typeof 3.14                // 返回 "number"
typeof (7)                 // 返回 "number"
typeof (7 + 8)             // 返回 "number"


undefined
在 JavaScript 中,没有值的变量,(技术上)其值是 undefined。typeof 也返回 undefined
任何变量均可通过设置值为 undefined 进行清空。其类型也将是 undefined。

var person;                  // 值是 undefined,类型是 undefined


空值
空值与 undefined 不是一回事
空的字符串变量既有值也有类型

var car = "";    // 值是 "",类型是 "string"


null
在 JavaScript 中,null 是 "nothing"。它被看做不存在的事物。
不幸的是,在 JavaScript 中,null 的数据类型是对象。
您可以把 null 在 JavaScript 中是对象理解为一个 bug。它本应是 null。

您可以通过设置值为 null 清空对象:

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};
person = null;    // 值是 null,但是类型仍然是对象
document.getElementById("demo").innerHTML = typeof person;    //object

您也可以通过设置值为 undefined 清空对象:

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};
person = undefined;    // 值是 undefined,类型是 undefined
document.getElementById("demo").innerHTML = person;    //undefined


Undefined 与 null 的值相等,但类型不相等:

typeof undefined              // undefined
typeof null                   // object
null === undefined            // false
null == undefined             // true


原始数据值是一种没有额外属性和方法的单一简单数据值。
typeof 运算符可返回以下原始类型之一:

  • string
  • number
  • boolean
  • undefined
typeof "Bill"              // 返回 "string"
typeof 3.14                // 返回 "number"
typeof true                // 返回 "boolean"
typeof false               // 返回 "boolean"
typeof x   


复杂数据
typeof 运算符可返回以下两种类型之一:

  • function
  • object

typeof 运算符把对象、数组或 null 返回 object。
typeof 运算符不会把函数返回 object。

typeof {name:'Bill', age:62} // 返回 "object"
typeof [1,2,3,4]             // 返回 "object" (并非 "array",在JavaScript数组即对象)
typeof null                  // 返回 "object"
typeof function myFunc(){}   // 返回 "function"

Guess you like

Origin www.cnblogs.com/leerep/p/12315481.html