有哪些JavaScript面试题是必须掌握的,能让我轻松应对面试?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2644个文字,预计阅读时间需要11分钟。
问题1:JavaScript中undefined和not defined的区别JavaScript中,未声明的变量使用typeof操作符会返回undefined,而尝试直接访问未声明的变量会抛出not defined错误。
未声明变量直接使用会抛出异常:var name is not defined如果不处理异常,代码将停止执行。
但是,使用typeof undeclared_variable不会抛出错误。
问题1:JavaScript 中 undefined 和 not defined 的区别
JavaScript 未声明变量直接使用会抛出异常:var name is not defined,如果没有处理异常,代码就停止运行了。 但是,使用typeof undeclared_variable并不会产生异常,会直接返回 undefined。
var x; // 声明 x console.log(x); //output: undefined console.log(typeof y); //output: undefined console.log(z); // 抛出异常: ReferenceError: z is not defined
问题2:下面的代码输出什么?
var y = 1; if (function f(){}) { y += typeof f; } console.log(y);
正确的答案应该是 1undefined。
本文共计2644个文字,预计阅读时间需要11分钟。
问题1:JavaScript中undefined和not defined的区别JavaScript中,未声明的变量使用typeof操作符会返回undefined,而尝试直接访问未声明的变量会抛出not defined错误。
未声明变量直接使用会抛出异常:var name is not defined如果不处理异常,代码将停止执行。
但是,使用typeof undeclared_variable不会抛出错误。
问题1:JavaScript 中 undefined 和 not defined 的区别
JavaScript 未声明变量直接使用会抛出异常:var name is not defined,如果没有处理异常,代码就停止运行了。 但是,使用typeof undeclared_variable并不会产生异常,会直接返回 undefined。
var x; // 声明 x console.log(x); //output: undefined console.log(typeof y); //output: undefined console.log(z); // 抛出异常: ReferenceError: z is not defined
问题2:下面的代码输出什么?
var y = 1; if (function f(){}) { y += typeof f; } console.log(y);
正确的答案应该是 1undefined。

