- 赋值表达式,返回的值是等号右边的值
console.log(a=3); //3
console.log(a='string'); ///
- 逻辑表达式:或‖返回第一个为真的表达式的值,与&&返回最后为真的表达式值
let a = 4;
let b = 5;
let c = false;
let d = true;
console.log(a || b); // 4
console.log(d || b); //true
console.log(a && b); // 5
console.log(c && b); //false
console.log(c || b); //5
逻辑表达式可以这么玩:
1.用在函数的执行上(通常不会这么用)
function hello(){
alert('hello!');
return true;
}
function hi(){
alert('hi');
return false;
}
console.log(hello() || hi()); //输出'hello'但是不输出'hi'
2.用在数值初始化上和赋值上
function a(obj){
let anObj = obj || {};
console.log(anObj);
}
a(); // {}
let b = {c:1};
a(b); // {c:1}
- 一个函数的默认返回值为undefined,否则为return的值
function hello () {
alert('hello!');
}
console.log("返回值为:"+hello()); //返回值为:undefined
- 算术表达式(加减乘除,自增和自减,负号前缀)返回计算得到的结果(不多解释了)
其他的没想到,想到接着更
网友评论