[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_operators [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript

JavaScript

JavaScript

 1   2

3+4 x*y

 

 

x++ ++x

= x = y y x

x = y x = y
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y
x **= y x = x ** y
x <<= y x = x << y
x >>= y x = x >> y
x >>>= y x = x >>> y
x &= y x = x & y
x ^= y x = x ^ y
x |= y x = x | y
x &&= f() x && (x = f())
x ||= f() x || (x = f())
x ??= f() x ?? (x = f())

Javascript

js
var foo = ["one", "two", "three"];

// 
var one = foo[0];
var two = foo[1];
var three = foo[2];

// 
var [one, two, three] = foo;

Unicode JavaScript === !==

js
var var1 = 3;
var var2 = 4;
true
Equal (==) true 3 == var1 "3" == var1 3 == '3'
Not equal (!=) true var1 != 4 var2 != "3"
Strict equal (===) true Object.is and sameness in JS. 3 === var1
Strict not equal (!==) true var1 !== "3" 3 !== '3'
Greater than (>) true var2 > var1 "12" > 2
Greater than or equal (>=) true var2 >= var1 var1 >= 3
Less than (<) true var1 < var2 "2" < 12
Less than or equal (<=) true var1 <= var2 var2 <= 5

=>

() (+ - * /)Infinity

js
1 / 2; // 0.5
1 / 2 == 1.0 / 2.0; // true

+ - * /JavaScript

% 12 % 5 2
++ ++xx++ var x=3; console.log(++x); //4 console.log(x); //4 var y=3; console.log(y++); //3 console.log(y); //4
-- var x=3; console.log(--x); // 2,x=2var y=3;console.log(y--);// 3,x=2;
- var x=3; console.log(-x); // -3
+ console.log( +'3' ); // 3 console.log( '3' ); // '3' console.log(+true); // 1
** baseexponent base^exponent 2 ** 3 810 ** -1 0.1

32 0 1 9 1001 JavaScript

JavaScript

Operator Usage Description
AND a & b a,b 1 1 0.
OR a | b a,b 1 1 0.
XOR a ^ b a,b 1 0.
NOT ~ a
shift a << b a b 0.
a >> b a b ( 0 1 )
( 0 ) a >>> b a b 0

  • 32bit 0 1 32bits 32bit
  Before: 11100110111110100000000000000110000000000001
  After:              10100000000000000110000000000001
  • ""

9 1001 15 1111.

15 & 9 9 1111 & 1001 = 1001
15 | 9 15 1111 | 1001 = 1111
15 ^ 9 6 1111 ^ 1001 = 0110
~15 -16 ~ 0000 0000 0000 1111 = 1111 1111 1111 0000
~9 -10 ~ 0000 0000 0000 1001 = 1111 1111 1111 0110

32 () 1 (2-)

32bit

<< 0 9<<2 36 1001 2 100100 36
>> 9>>2 2 1001 2 10 2-9>>2 -3
>>> 0 19>>>2 4 10011 2 100 4

; &&||

(&&) expr1 && expr2 () expr1 false expr1expr2&& true true false.
(||) expr1 || expr2 () expr1 true expr1expr2|| true true false false
(!) !expr () true false true

falsenull, 0, NaN, ("") undefinedfalsy

&&""

js
var a1 = true && true; // t && t returns true
var a2 = true && false; // t && f returns false
var a3 = false && true; // f && t returns false
var a4 = false && 3 == 4; // f && f returns false
var a5 = "Cat" && "Dog"; // t && t returns Dog
var a6 = false && "Cat"; // f && t returns false
var a7 = "Cat" && false; // t && f returns false

||""

js
var o1 = true || true; // t || t returns true
var o2 = false || true; // f || t returns true
var o3 = true || false; // t || f returns true
var o4 = false || 3 == 4; // f || f returns false
var o5 = "Cat" || "Dog"; // t || t returns Cat
var o6 = false || "Cat"; // f || t returns Cat
var o7 = "Cat" || false; // t || f returns Cat

""

js
var n1 = !true; // !t returns false
var n2 = !false; // !f returns true
var n3 = !"Cat"; // !t returns false

  • false && anything // false
  • true || anything // true

anything

+

js
console.log("my " + "string"); // console logs the string "my string".

+=

js
var myString = "alpha";

myString += "bet"; //  "alphabet"

JavaScript

 ?  1 :  2

1 2

js
var status = age >= 18 ? "adult" : "minor";

age 18 adult statusminor status

, for

a 10

js
var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var a = [x, x, x, x, x];

for (var i = 0, j = 9; i <= j; i++, j--)
  console.log("a[" + i + "][" + j + "]= " + a[i][j]);

delete

delete

js
delete objectName.property;
delete objectName[index];
delete property; // legal only within a with statement

objectNameproperty index

with

delete var

delete undefined deletetruefalse

js
x = 42;
var y = 43;
myobj = new Number();
myobj.h = 4; // create property h
delete x; // returns true (can delete if declared implicitly)
delete y; // returns false (cannot delete if declared with var)
delete Math.PI; // returns false (cannot delete predefined properties)
delete myobj.h; // returns true (can delete user-defined properties)
delete myobj; // returns true (can delete if declared implicitly)

a[3], a[4]a[4] a[3] undefined

delete trees[3]trees[3] undefined

js
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
if (3 in trees) {
  // 
}

undefinedundefineddelete trees[3] undefined,

js
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
if (3 in trees) {
  // this gets executed
}

typeof

typeof 2

js
typeof operand;
typeof (operand);

typeof operand operand operand

js
var myFun = new Function("5 + 2");
var shape = "round";
var size = 1;
var today = new Date();

typeof

js
typeof myFun; // returns "function"
typeof shape; // returns "string"
typeof size; // returns "number"
typeof today; // returns "object"
typeof dontExist; // returns "undefined"

true null typeof

js
typeof true; // returns "boolean"
typeof null; // returns "object"

typeof

js
typeof 62; // returns "number"
typeof "Hello world"; // returns "string"

typeof

js
typeof document.lastModified; // returns "string"
typeof window.length; // returns "number"
typeof Math.LN2; // returns "number"

typeof

js
typeof blur; // returns "function"
typeof eval; // returns "function"
typeof parseInt; // returns "function"
typeof shape.split; // returns "function"

typeof

js
typeof Date; // returns "function"
typeof Function; // returns "function"
typeof Math; // returns "object"
typeof Option; // returns "function"
typeof String; // returns "function"

void

void

js
void expression;
void (expression);

void expression javaScript

void

html
<a href="javascript:void(0)">Click here to do nothing</a>

html
<a href="javascript:void(document.form.submit())">Click here to submit</a>

in

intrue

js
propNameOrNumber in objectName;

propNameOrNumberobjectName

in

js
// Arrays
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees; // returns true
3 in trees; // returns true
6 in trees; // returns false
"bay" in trees; // returns false (you must specify the index number,
// not the value at that index)
"length" in trees; // returns true (length is an Array property)

// Predefined objects
"PI" in Math; // returns true
var myString = new String("coral");
"length" in myString; // returns true

// Custom objects
var mycar = { make: "Honda", model: "Accord", year: 1998 };
"make" in mycar; // returns true
"model" in mycar; // returns true

instanceof

true

js
objectName instanceof objectType;

objectName objectTypeDate Array.

instanceof. catch

instanceof theDay Date theDay Date if

js
var theDay = new Date(1995, 12, 17);
if (theDay instanceof Date) {
  // statements to execute
}

Operator type Individual operators
member . []
call / create instance () new
negation/increment ! ~ - + ++ -- typeof void delete
multiply/divide * / %
addition/subtraction + -
bitwise shift << >> >>>
relational < <= > >= in instanceof
equality == != === !==
bitwise-and &
bitwise-xor ^
bitwise-or |
logical-and &&
logical-or ||
conditional ?:
assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
comma ,

JavaScript

x=7 = 7 x 7

3 + 4 + 3 4 7

JavaScript

  • 3.14159
  • "Fred" "234"
  • true false
  • javascript

this

thisthis

js
this["propertyName"];
this.propertyName;

valuevalidate

js
function validate(obj, lowval, hival) {
  if (obj.value < lowval || obj.value > hival) console.log("Invalid Value!");
}

onchangevalidatthis

html
<p>Enter a number between 18 and 99:</p>
<input type="text" name="age" size="3"  />

js
var a = 1;
var b = 2;
var c = 3;

// 
a + b * c; // 7
// 
a + b * c; // 7

// 
(a + b) * c; // 9

// 
a * c + b * c; // 9

Comprehensions JavaScript ECMAScript comprehensions:

[for (x of y) x]

(for (x of y) y)

Comprehensions

js
[for (i of [ 1, 2, 3 ]) i*i ];
// [ 1, 4, 9 ]

var abc = [ "A", "B", "C" ];
[for (letters of abc) letters.toLowerCase()];
// [ "a", "b", "c" ]

new

new

js
var objectName = new objectType([param1, param2, ..., paramN]);

super

super

super([arguments]); // calls the parent constructor. super.functionOnParent([arguments]);

Web Proxy Viewer  |  New URL  |  Original Page