Author: fatfish
Translator: Front-end Xiaozhi Source: medium
If you have dreams and dry goods, you can search for [Great Move to the World] on WeChat and pay attention to this Shuwanzhi who is still washing dishes in the early hours of the morning.
This article GitHub https://github.com/qq449245884/xiaozhi has been included, there are complete test sites, materials and my series of articles for interviews with first-line manufacturers.
foreword
Recently, I was asked a few weird interview questions. They are different from normal questions: these interview questions seem very simple, but they test your thorough understanding of JavaScript. How many can you answer correctly?
x !== x can be true ?
Excuse x
will be printed 大迁世界
const x = ? // ??
if (x !== x) {
console.log('大迁世界')
}
Strange, is there any value that is not equal to itself? There is indeed a value in JS NaN
which is not equal to any value, not even itself.
const x = NaN
if (x !== x) {
console.log('大迁世界')
}
console.log(NaN === NaN) // false
console.log(x !== x) // true
console.log(Number.isNaN(x)) // true
(!isNaN(x) && x !== x) can be true?
Now that we rule out NaNs, what other value can be not equal to itself?
const x = ?
if(!isNaN(x) && x !== x) {
console.log('hello fatfish')
}
view rawq2-1.js hosted with
Maybe you know object. Defineproperty
which can help us solve this problem.
window.x = 0
Object.defineProperty(window, 'x', {
get () {
return Math.random()
}
})
console.log(x) // 0.12259077808826002
console.log(x === x) // false
console.log(x !== x) // true
3. How to make x === x + 1
This question may not be easy, but as long as you know JS, you will know Number.MAX_SAFE_INTEGER
, which means that the constant represents the largest safe integer in JavaScript ( 2^53 - 1
).
So, we can assign Number.MAX_SAFE_INTEGER
to x
:
const x = Number.MAX_SAFE_INTEGER + 1
if (x === x + 1) {
console.log('大迁世界')
}
4. x > x
can be true
?
I don't want to read any more, what rubbish question is this?
const x = ?
if (x > x) {
console.log('hello fatfish')
}
Although it seems unlikely, how can a value be greater than itself? However, we can use the Symbol.toPrimitive
function to do this.
const x = {
value: 1,
[ Symbol.toPrimitive ] () {
console.log('x', this.value)
return --this.value
}
}
if (x > x) {
console.log('大迁世界')
}
Really amazing (garbage) odd (garbage).
5. typeof x === 'undefined' && x.length > 0
const x = ?
if(typeof x === 'undefined' && x.length > 0) {
console.log('大迁世界')
}
I have to admit, JS is an amazing language. Besides undefined
itself, what value could make typeof x === undefined
to be true
?
The answer is document.all
, which represents all elements on the page.
const x = document.all
if(typeof x === 'undefined' && x.length > 0) {
console.log('大迁世界')
}
console.log(x)
console.log(typeof x)
console.log(x === undefined)
At last
What strange interview questions have you encountered, please leave a message to add! !
The bugs that may exist in editing cannot be known in real time. In order to solve these bugs afterwards, a lot of time is spent on log debugging. By the way, here is a useful BUG monitoring tool , Fundebug .
Original: https://javascript.plnenglish.io/interviewer-can-xx-return-true-in-javascript-7e1d1a7b5cd
comminicate
If you have dreams and dry goods, you can search for [Great Move to the World] on WeChat and pay attention to this Shawanzhi who is still washing dishes in the early hours of the morning.
This article GitHub https://github.com/qq449245884/xiaozhi has been included, there are complete test sites, materials and my series of articles for interviews with first-line manufacturers.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。