xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js 运算符的执行顺序

js 运算符的执行顺序

js 运算符优先级

Operator Precedence

下表从最高(21)到最低(1)优先顺序排列

left-to-right

从左到右 / 先左后右

right-to-left

从右到左 / 先右后左

逻辑非
一元加减
一元加 / 一元减

Precedence & Associativity

优先级 & 关联性

let a, b;

a = b = 5;
// same as writing
a = (b = 5);


a || (b * c);  // evaluate `a` first, then produce `a` if `a` is "truthy"
a && (b < c);  // evaluate `a` first, then produce `a` if `a` is "falsy"
a ?? (b || c); // evaluate `a` first, then produce `a` if `a` is not `null` and not `undefined`
a?.b.c;        // evaluate `a` first, then produce `undefined` if `a` is `null` or `undefined`

demo


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-10-01
 * @modified
 *
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

// console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]);

// log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]);

log([]);
log([[]]);
log(+[]);

log([][[]]);

log(([][[]]+[]));


log([+!![]]);
log(!![]);
log(+!![]);

log(`\n+true =`, +true)


// log(([][[]]+[])[]);


// log((undefined)[0])
// log((undefined)[1])

log(+([]+{}));

log([]+{});

log(+[]);
// 0
log(!+[]);
// true
log(`\n!0 =`, !0);
// true

log(!![]);
// true
log(+!![]);
// 1
log(`\n+true =`, +true);
// 1


log(!+[]+!![]);
// 2
log(`\ntrue + true =`, true + true);
// 2
log([!+[]+!![]]);
// [2]


log(``, +([]+{})[!+[]+!![]])
// NaN
log(``, ([]+{})[!+[]+!![]])
// "b"


refs

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-10-22 21:58  xgqfrms  阅读(392)  评论(2编辑  收藏  举报