Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are 7 simple but thorny JavaScript interview questions?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how 7 simple but difficult JavaScript interview questions are. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.

If you qualify as a senior developer working with JavaScript, you are likely to be asked tough questions during your coding interview.

Follow this advice:"Practice makes perfect." Learning JavaScript deeply and regularly will improve your coding skills and can improve your interview skills.

In this article, you'll find 7 JavaScript interview questions that seem simple at first glance but are tricky.

While these questions may seem random at first, they attempt to hook up with important concepts in JavaScript. So you'd better practice before your next interview!

1. unexpected global variable

Question

The following code snippet is used to calculate type of a and type of b:

function foo() { let a = b = 0; a++; return a; } foo(); typeof a; // => ??? typeof b; // => ???

Answer

Let's look at line 2: let a = b = 0, which declares a local variable a, but it also declares a global variable b.

Variable b is not declared in either foo() scope or global scope. JavaScript therefore interprets the b = 0 expression as window.b = 0. In other words, b is an accidentally created global variable.

In the browser, the above code snippet is equivalent to:

function foo() { let a; window.b = 0; a = window.b; a++; return a; } foo(); typeof a; // => 'undefined' typeof window.b; // => 'number'

typeof a equals 'undefined' and the variable a exists in foo() and is not used in the outer scope.

Because b is a global variable with a value of 0, the type of b has a value of 'number'.

2. The length property of an array

Question

What is the value of clothes[0]?

const clothes = ['jacket', 't-shirt']; clothes.length = 0; clothes[0]; // => ???

Answer

The length attribute of an array object has a special behavior: decreasing the value of the length attribute has the side effect of deleting its own array elements. Therefore, JavaScript executes clothes.length = 0 to delete all elements.

clothes [0] equals undefined because the clothes array has been emptied.

3. Eagle Eye Test

Question

What are the contents of the numbers array?

const length = 4; const numbers = []; for (var i = 0; i

< length; i++);{ numbers.push(i + 1); } numbers; // =>

???

Let's take a closer look at the semicolon; it appears in left braces {:

This semicolon is easy to ignore and creates an empty statement. An empty statement is an empty statement that does nothing.

For() iterates 4 times over an empty statement (does nothing), ignoring the block that actually pushes the item into the array: {number.push (i + 1);}. The above code is equivalent to the following code:

const length = 4; const numbers = []; var i; for (i = 0; i

< length; i++) { // does nothing } { // a simple block numbers.push(i + 1); } numbers; // =>

[5]

For() increments the i variable to 4, and JavaScript enters the block {number.push (i + 1);} once, pushing 4 + 1 into the number array.

Therefore, the contents of the numbers array are [5].

4. automatic semicolon insertion

Question

arrayFromValue() returns what value? function arrayFromValue(item) { return [item]; } arrayFromValue(10); // => ???

Answer

It's easy to miss the newline between the return keyword and the [item] expression. This newline causes JavaScript to automatically insert a semicolon between the return and [item] expressions.

This is the equivalent code, inserting a semicolon after the return:

function arrayFromValue(item) { return; [item]; } arrayFromValue(10); // => undefined

return; function internally causes it to return undefined. Therefore arrayFromValue(10) is undefined.

5. Classic Problem: Tough Closure

Question

What does the following code print to the console?

let i; for (i = 0; i

< 3; i++) { const log = () =>

{ console.log(i); } setTimeout(log, 100); }

Answer

If you've never heard of this tricky question before, chances are your answers are 0, 1, and 2: it's wrong. When I first tried to solve it, that was my answer too!

There are two stages to executing this code segment.

Phase 1

HarmonyOS Technology Community

For() is repeated three times, and during each iteration a new function log() is created to capture the variable i. Then setTimout() plans to execute log().

When the for() loop is complete, the value of the i variable is 3.

log() is a closure that captures the variable i defined in the outer scope of the for() loop. It is important to note that closures can lexically capture i variables.

Phase 2

The second phase occurs 100ms later: setTimeout() calls log() of the schedule 3 times. log() reads the current value of variable i as 3 and logs it to console 3. This is why the console outputs 3, 3, and 3.

If you have trouble understanding closures, it is recommended to read "A Simple Description of JavaScript Closures."

Do you know how to record code snippets as 0, 1, and 2? Please write your solution in the comments below!

6. floating point number computations

Question

What is the result of this equation?

0.1 + 0.2 === 0.3 // => ???

Answer

First, let's look at the value of 0.1 + 0.2:

0.1 + 0.2; // => 0.30000000000000004

0.1 The sum of 0 and 0.2 is not exactly 0.3, but slightly higher.

Because floating-point numbers are encoded in binary, operations such as adding floating-point numbers produce rounding errors.

In short, direct comparison of floating-point numbers is inaccurate.

So 0.1 + 0.2 === 0.3 results in false.

Click 0.30000000000000004.com for more information.

7. variable lift

Question

What happens if myVar and myConst are accessed before the declaration?

myVar; // => ??? myConst; // => ??? var myVar = 'value'; const myConst = 3.14;

Answer

Lifts and temporary dead zones are two important concepts that affect the life cycle of JavaScript variables.

The result of accessing myVar before declaration is undefined. Before initialization, the promoted var variable has a value undefined.

However, accessing myConst before the declaration line raises a ReferenceError. The const variable is in temporary dead zone until the declaration line const myConst = 3.14.

You can think of certain questions as useless for an interview. I feel the same way, especially about the Hawkeye test. However, they may be questioned.

In any case, many of these questions can really assess whether you are proficient in JavaScript, such as tricky closures.

So here are 7 simple but tricky JavaScript interview questions: How did you learn knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report