每日3题

1 以下代码执行后,控制台中的输入内容为?

console.log(+true, !'hello')

2 点击p标签时,会输入什么

const numbers=[1,2,3,4,5]const [y] = numbersconsole.log(y)

3 以下代码执行后,控制台中的输入内容为?

const fn = (x) => (y) => (z) => {  console.log(x, y, z);};fn(1)(2)(3);
  • 公众号【明天也要写bug】更多前端面试题

答案及解析

1

// 答案:1 false// 考查运算符和类型转换// 相加运算符:如果+两边有字符串,则将两者拼接为字符串,否则进行相加运算// +true,会将 true 转换成数字 1,最终输入 1console.log(+true, !"hello");// 在 JS 中,假值有:null;NaN;0;空字符串 ("" or '' or ``);undefined.// 除此之外都是真值// !真值 => 假值,!假值 => 真值// 所以 !"hello" 为 false

2

// 答案:1// 考查 ES6 数组的解构赋值// ES6 容许写成这样:// let [a, b, c] = [1, 2, 3]// 这种写法能够从数组中提取值,依照对应地位,对变量赋值const numbers = [1, 2, 3, 4, 5];const [y] = numbers; // y 对应 1console.log(y); // 故输入 1

3

// 答案:1 2 3// 考查箭头函数的闭包和函数调用的联合性const fn = (x) => (y) => (z) => {  console.log(x, y, z);};// fn 相当于:const fn = (x) => {  return (y) => {    return (z) => {      console.log(x, y, z);    };  };};fn(1)(2)(3); // 函数调用的联合性是从左到右,等价于const f1 = fn(1); // (y) => {...}const f2 = f1(2); // (z) => {  console.log(x, y, z); }f2(3); // 输入 1 2 3