Home Web Front-end JS Tutorial Summary of JavaScript development interview questions

Summary of JavaScript development interview questions

Apr 02, 2018 pm 05:57 PM
javascript interview

This article shares with you a summary of JavaScript development interview questions. The content is quite good. I hope it can help friends in need

No1. Grammar and types

1. Declaration and definition

Variable type: var, defines variables; let, defines block domain (scope) local variables; const, defines read-only constants.

Variable format: starts with a letter, underscore "_" or $ symbol, case sensitive.

Variable assignment: A variable that is declared but not assigned has a value of undefined when used. An exception will be thrown if an undeclared variable is used directly.

Unassigned variable for calculation: the result is NaN. For example:

var x, y = 1;
console.log(x + y); //结果为NaN,因为x没有赋值。
Copy after login

2. Scope

Variable scope: Before ES6, there was no block declaration scope, and variables acted in function blocks or globally. As shown in the following code, the input x is 5.

if (true) {var x = 5;
}
console.log(x); // 5
Copy after login

ES6 variable scope: ES6 supports block scope, but you need to use let to declare variables. The following code output results in an exception being thrown.

f (true) {let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234
Copy after login

Variable floating: In a method or global code, when we use a variable before the variable is declared, an exception is not thrown, but undefined is returned. This is because JavaScript automatically floats variable declarations to the front of functions or globals. For example, the following code:

/**
* 全局变量上浮
*
/console.log(x === undefined); // logs "true"
var x = 3;

/**
* 方法变量上浮
*/
var myvar = "my value";
// 打印变量myvar结果为:undefined
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})();
Copy after login

The above code and the following code are equivalent:

/**
* 全局变量上浮
*/var x;
console.log(x === undefined); // logs "true"x = 3;/**
* 方法变量上浮
*/var myvar = "my value";
(function() {var myvar;
console.log(myvar); // undefinedmyvar = "local value";
})();
Copy after login

Global variables: In the page, the global object is window, so we can access global variables through window. variable. For example:

version = "1.0.0";
console.log(window.version); //输出1.0.0
Copy after login

No2. Data structure and type

1.数据类型

6个基础类型:Boolean(true或者false)、null(js大小写敏感,和Null、NULL是有区别的)、undefined、Number、String、Symbol(标记唯一和不可变)

一个对象类型:object。

object和function:对象作为值的容器,函数作为应用程序的过程。

2.数据转换

函数:字符串转换为数字可使用parseInt和parseFloat方法。

parseInt:函数签名为parseInt(string, radix),radix是2-36的数字表示数字基数,例如十进制或者十六进制。返回结果为integer或者NaN,例如下面输出结果都为15。

parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
arseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
Copy after login

parseFloat:函数签名为parseFloat(string),返回结果为数字或者NaN。例如:

parseFloat("3.14"); //返回数字
parseFloat("314e-2"); //返回数字
parseFloat("more non-digit characters"); //返回NaN
Copy after login

3.数据类型文本化

文本化类型:Array、Boolean、Floating-point 、integers、Object、RegExp、String。

Array中额外的逗号情况:["Lion", , "Angel"],长度为3,[1]的值为undefiend。['home', , 'school', ],最后一个逗号省略所以长度为3。[ , 'home', , 'school'],长度为4。['home', , 'school', , ],长度为4。

integer整数:整数可以表达为十进制、八进制、十六进制、二进制。例如:

0, 117 and -345 //十进制
015, 0001 and -0o77 //八进制
0x1123, 0x00111 and -0xF1A7 //十六进制
0b11, 0b0011 and -0b11 1234 //二进制
Copy after login

浮点数:[(+|-)][digits][.digits][(E|e)[(+|-)]digits]。例如:

3.1415926,-.123456789,-3.1E+12(3100000000000),.1e-23(1e-24)
Copy after login

对象:对象的属性获取值可通过“.属性”或者“[属性名]”获取。例如:

var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };
console.log(car.manyCars.b); // Jeep
console.log(car[7]); // Mazda
Copy after login

对象属性:属性名可以是任意字符串或者空字符串,无效的名字可通过引号包含起来。复杂的名字不能通过.获取,但可以通过[]获取。例如:

var unusualPropertyNames = {
"": "An empty string",
"!": "Bang!"
}
console.log(unusualPropertyNames.""); // SyntaxError: Unexpected string
console.log(unusualPropertyNames[""]); // An empty string
console.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !
console.log(unusualPropertyNames["!"]); // Bang!
Copy after login

转意字符:下面的字符串输出结果包含了双引号,因为使用了转意符号“\””。

var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
console.log(quote);
/输出:He read "The Cremation of Sam McGee" by R.W. Service.1。
Copy after login

字符串换行法:直接在字符串行结束时添加“\”,如下代码所示:

var str = "this string \
is broken \
across multiple\
lines."
console.log(str); // this string is broken across multiplelines.
Copy after login

No3.控制流和错误处理

1.块表达式

作用:块表达式一般用于控制流,像if、for、while。下面的代码中{x++;}就是一个块声明。

while (x < 10) {x++;
}
Copy after login

ES6之前没有块域范围:在ES6之前,在block中定义的变量实际是包含在方法或者全局中,变量的影响超出了块作用域的范围。例如下面的代码最终执行结果为2,因为block中声明的变量作用于方法。

var x = 1;
{var x = 2;
}
console.log(x); // outputs 2
Copy after login

ES6之后有块域范围:在ES6中,我们可以把块域声明var改成let,让变量只作用域block范围。

var b = new Boolean(false);
if (b) // 返回true
if (b == true) // 返回false
Copy after login

No4.异常处理

1.异常类型

抛出异常语法:抛异常可以是任意类型。如下所示。

throw "Error2"; // 字符串类型
throw 42; // 数字类型
throw true; // 布尔类型
throw {toString: function() { return "I&#39;m an object!"; } }; //对象类型
Copy after login

自定义异常:

// 创建一个对象类型UserException
function UserException(message) {
this.message = message;
this.name = "UserException";
}

//重写toString方法,在抛出异常时能直接获取有用信息
UserException.prototype.toString = function() {
return this.name + &#39;: "&#39; + this.message + &#39;"&#39;;
}
// 创建一个对象实体并抛出它
throw new UserException("Value too high");
Copy after login

2.语法

关键字:使用try{}catch(e){}finally{}语法,和C#语法相似。

finally返回值:如果finaly添加了return 语句,则不管整个try.catch返回什么,返回值都是finally的return。如下所示:

function f() {
    try {
        console.log(0);        throw "bogus";
    } catch(e) {
        console.log(1);        return true; // 返回语句被暂停,直到finally执行完成
        console.log(2); // 不会执行的代码
    } finally {
        console.log(3);        return false; //覆盖try.catch的返回
        console.log(4); //不会执行的代码
    }    // "return false" is executed now 
    console.log(5); // not reachable}
f(); // 输出 0, 1, 3; 返回 false
Copy after login

finally吞并异常:如果finally有return并且catch中有throw异常。throw的异常不会被捕获,因为已经被finally的return覆盖了。如下代码所示:

function f() {
    try {        throw "bogus";
    } catch(e) {
        console.log(&#39;caught inner "bogus"&#39;);        throw e; // throw语句被暂停,直到finally执行完成
    } finally {        return false; // 覆盖try.catch中的throw语句
    }    // 已经执行了"return false"}try {
    f();
} catch(e) {    //这里不会被执行,因为catch中的throw已经被finally中的return语句覆盖了
    console.log(&#39;caught outer "bogus"&#39;);
}// 输出// caught inner "bogus"
Copy after login

系统Error对象:我们可以直接使用Error{name, message}对象,例如:throw (new Error(‘The message’));

The above is the detailed content of Summary of JavaScript development interview questions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Selected Java JPA interview questions: Test your mastery of the persistence framework Selected Java JPA interview questions: Test your mastery of the persistence framework Feb 19, 2024 pm 09:12 PM

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

See all articles