欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js表达式与运算符代码讲解

程序员文章站 2023-12-25 22:03:45
js表达式与运算符代码讲解 /** * created by administrator on 2017/12/14. * 表达式与运算符 */ //1.基本表...

js表达式与运算符代码讲解

/**
 * created by administrator on 2017/12/14.
 * 表达式与运算符
 */
//1.基本表达式  加减乘除
var a = 4;
a = 7/6;
var b = (a + 4)/2;
console.log(a);
console.log(b);
console.log("==========");
//end

//2.比较运算符 == != > < >= <=
console.log(a <= b);
console.log(a == b);
console.log(a != b);
console.log("==========");
//end

//3.逻辑表达式 逻辑与(&&)和逻辑或(||)
var ret = (a > 1) && (a < 2);
console.log(ret);
console.log(true && false);
console.log("==========");
//end

//4.字符串
a = 10;
b = 20;
var num_str = "" + a + b + true;
console.log(num_str);   //1020true
num_str = a + b + true + "";
console.log(num_str);   //31
console.log("==========");
//end

//5.简约表达式  += -= *= /=  ++  --
var c = null;
a += c; //a = a + c
a -= c; //a = a - c
a *= c; //a = a * c
a /= c; //a = a / c

a = 10;
a++;    //a = a + 1   先参与运算,最后自增
++a;    //a = a + 1   先自增在参与运算
console.log(a);
a--;    //a = a - 1   先参与运算,最后自减
console.log(a);
--a;    //a = a - 1   先自减在参与运算

a = 4;
console.log(a++);
console.log(++a);
// end

上一篇:

下一篇: