微信小程序内常用的运算符有哪些
基本运算符示例代码:var a = 10, b = 20;// 加法运算console.log(30 === a + b);// 减法运算console.log(-10 === a - b);// 乘法运算console.log(200 === a * b);// 除法运算console.log(0.5 === a / b);// 取余运算console.log(10 === a % b);加法运算(+)也可以用作字符串的拼接。var a = '.w' , b = 'xs';// 字符串拼接console.log('.wxs' === a + b);一元运算符示例代码:var a = 10, b = 20;// 自增运算console.log(10 === a++);console.log(12 === ++a);// 自减运算console.log(12 === a--);console.log(10 === --a);// 正值运算console.log(10 === +a);// 负值运算console.log(0-10 === -a);// 否运算console.log(-11 === ~a);// 取反运算console.log(false === !a);// delete 运算console.log(true === delete a.fake);// void 运算console.log(undefined === void a);// typeof 运算console.log("number" === typeof a);位运算符示例代码:var a = 10, b = 20;// 左移运算console.log(80 === (a << 3));// 无符号右移运算console.log(2 === (a >> 2));// 带符号右移运算console.log(2 === (a >>> 2));// 与运算console.log(2 === (a & 3));// 异或运算console.log(9 === (a ^ 3));// 或运算console.log(11 === (a | 3));比较运算符示例代码:var a = 10, b = 20;// 小于console.log(true === (a < b));// 大于console.log(false === (a > b));// 小于等于console.log(true === (a <= b));// 大于等于console.log(false === (a >= b));等值运算符示例代码:var a = 10, b = 20;// 等号console.log(false === (a == b));// 非等号console.log(true === (a != b));// 全等号console.log(false === (a === b));// 非全等号console.log(true === (a !== b));赋值运算符示例代码:var a = 10;a = 10; a *= 10;console.log(100 === a);a = 10; a /= 5;console.log(2 === a);a = 10; a %= 7;console.log(3 === a);a = 10; a += 5;console.log(15 === a);a = 10; a -= 11;console.log(-1 === a);a = 10; a <<= 10;console.log(10240 === a);a = 10; a >>= 2;console.log(2 === a);a = 10; a >>>= 2;console.log(2 === a);a = 10; a &= 3;console.log(2 === a);a = 10; a ^= 3;console.log(9 === a);a = 10; a |= 3;console.log(11 === a);二元逻辑运算符示例代码:var a = 10, b = 20;// 逻辑与console.log(20 === (a && b));// 逻辑或console.log(10 === (a || b));其他运算符示例代码:var a = 10, b = 20;//条件运算符console.log(20 === (a >= 10 ? a + 10 : b + 10));//逗号运算符console.log(20 === (a, b));运算符优先级优先级运算符说明结合性20( ... )括号n/a19... . ...成员访问从左到右 ... [ ... ]成员访问从左到右 ... ( ... )函数调用从左到右17... ++后置递增n/a ... --后置递减n/a16! ...逻辑非从右到左 ~ ...按位非从右到左 + ...一元加法从右到左 - ...一元减法从右到左 ++ ...前置递增从右到左 -- ...前置递减从右到左 typeof ...typeof从右到左 void ...void从右到左 delete ...delete从右到左14... * ...乘法从左到右 ... / ...除法从左到右 ... % ...取模从左到右13... + ...加法从左到右 ... - ...减法从左到右12... << ...按位左移从左到右 ... >> ...按位右移从左到右 ... >>> ...无符号右移从左到右11... < ...小于从左到右 ... <= ...小于等于从左到右 ... > ...大于从左到右 ... >= ...大于等于从左到右10... == ...等号从左到右 ... != ...非等号从左到右 ... === ...全等号从左到右 ... !== ...非全等号从左到右9... & ...按位与从左到右8... ^ ...按位异或从左到右7... | ...按位或从左到右6... && ...逻辑与从左到右5... || ...逻辑或从左到右4... ? ... : ...条件运算符从右到左3... = ...赋值从右到左 ... += ...赋值从右到左 ... -= ...赋值从右到左 ... *= ...赋值从右到左 ... /= ...赋值从右到左 ... %= ...赋值从右到左 ... <<= ...赋值从右到左 ... >>= ...赋值从右到左 ... >>>= ...赋值从右到左 ... &= ...赋值从右到左 ... ^= ...赋值从右到左 ... |= ...赋值从右到左0... , ...逗号从左到右相信看了本文案例你已经掌握了方法,更多精彩请关注小潘博客其它相关文章!
推荐阅读:
微信小程序开发中怎样实现Android环境下的横向滚动以上就是微信小程序内常用的运算符有哪些的详细内容,更多请关注小潘博客其它相关文章!