DEV Community

Cover image for 9 Common JavaScript Interview Questions You Might Not Know The Answer For.

9 Common JavaScript Interview Questions You Might Not Know The Answer For.

Gus Pear 🍐 on January 14, 2023

Whether you like them or not, tricky questions are still asked by interviewers in the wild. The reason is, these questions can tell a lot about yo...
Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi

I will leave this here 🫣:

> function isInt(num) {
...   return num % 1 === 0;
... }
undefined
> isInt(1.00000000000000001)
true
> isInt(1.0000000000000001)
true
> isInt(1.000000000000001)
false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gustavupp profile image
Gus Pear 🍐

Hi Ervin!

Thanks for taking the time to comment.

I did a quick research and turns out JavaScript has a precision limit when it comes to numbers.

Here's what the MDN's explanation:
Image description

Thanks again for raising the issue.

Have a great week

Gus

Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi • Edited

Yes, this is why the builtin Number.isInteger fails with the same values:

>Number.isInteger(1.00000000000000001)
true
>Number.isInteger(1.0000000000000001)
true
>Number.isInteger(1.000000000000001)
false
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
gustavupp profile image
Gus Pear 🍐

That's right Ervin

Collapse
 
ravavyr profile image
Ravavyr

See, I'm bad with terminology and my javascript is self-taught, so with 16 years experience i can pretty much build anything on the internet [given enough time and resources], but i can't answer silly questions about closures and type that have zero effect on real world day to day projects.

Ask me how I'd write code to meet a request and how I'd provide a deliverable.
Ask me how I check for code performance or responsiveness [how many of you have code that fires on load but breaks when a browser window is resized? do you even know? do you even resize while testing?]
Give me a business problem and ask me what i'd write to solve it.
Give me data and tell me what you want to show on the interface and include some additional behind the scenes logic that needs to be performed on the data to meet the interface requirements.
Ask me what i do if there's a bug in the code. Present me with a bug and let me debug it, track down and solve the problem.
Ask me real world questions, not book questions.

If i want to know which type is undefined...I run the code.
I wouldn't write a multiple function because i can put that one line inside another function as needed
If i want to write a certain piece of logic....I write that piece of logic without first debating whether one term outweighs another term.
If i want to know what code performs better, i write both options and run them side by side to see since in the real world code performance also depends on the data its processing and what devices you're running it on.
That "delete" stuff though...man that's gonna ruin anyone's life, cuz you don't use it often and when you do it just doesn't work like you'd expect and I wish the people developing javascript would fix odd behaviors like that instead of shoving more and more Type nonsense into the language just because it seems devs can't be expected to write code with a few extra if statements to handle unexpected types?

Code interviews kill me. Good article though.

Collapse
 
gustavupp profile image
Gus Pear 🍐

I loved your comment Ravavyr!

I understand how you feel about coding interviews, cuz I feel the same way.

Unfortunately, we have to play by their rules if we want a better job.

Thanks for sharing your thoughts on this topic.

Enjoy the rest of your week Ravavyr.

Off-topic: Your writing is good as. Funny, witty, and has a ton of personality (I'll get there)

Collapse
 
ysmnikhil profile image
Nikhil Malik

Ravavyr, I agree with you.

From your comment, I can see, you have lots of amazing things in mind and which could become a series series of blogs.

If yes, I will be eagerly waiting for that ;)

Collapse
 
tracygjg profile image
Tracy Gilmore • Edited

Hi Gus,
I think you have a few typos in the section covering "delete operator on Object"

The prototype object Person and the instance person1 would have a property of age but not Age so delete person1.age would have the intended result but console.log(person1.Age) would be undefined.

Also, in the description there is a reference to 'emp1' but that is not referenced in the example. It also states "has it's 'name' property deleted" when it does not, it is the age property that is deleted.

I suggest replacing the text for:
When the instance 'person1' has its 'age' property deleted, we can still access the 'age' property of the prototype object(Person).

Best regards.

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hey Gilmore!! How are you doing?

Thanks again for stopping by, catching my mistakes, and improving the post.

The typos have been corrected.

Have a wonderful week.

Collapse
 
moopet profile image
Ben Sinclair

Most of these seem to be, "this code is deliberately broken, how could you make it work?" type questions, and they're a bit contrived. The solution to all of them is, "write clean, self-documenting code that doesn't try to be too clever".

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hey Ben, how are you?

I agree 100% with what you said.

The phrase: "write clean, self-documenting code that doesn't try to be too clever", solves 99% of the problems.

Thanks for your comment.

Have a great week.

Collapse
 
carlosds profile image
Karel De Smet

The simplest and most correct way to check if a number is an integer is Number.isInteger()

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hi Karel! How are you doing?

You are right.

I'll add Number.isInteger() to the examples.

Thanks for stopping by and improving the post.

Hope to see you around again.

Have a great week!

Collapse
 
dpagey profile image
Pagey

"Common interview questions" 🤔

Good article but these questions probably shouldn't come up in interviews. There are much more relevant questions for the day-to-day job.

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hi Pagey! Thanks for taking the time to comment.

They are definitely relevant for the day-to-day but have also come up in interviews in the past.

Enjoy the rest of your week

Collapse
 
dpagey profile image
Pagey

"definitely"...sure

Collapse
 
raguram90 profile image
RamR

thanks. very useful article. got better understanding of closures now.

Collapse
 
gustavupp profile image
Gus Pear 🍐

I am glad it was helpful RaguRam!

Thanks for your comment!

Enjoy your sunday

Collapse
 
milandry profile image
Michael Landry

After reading this post, I get the sense that people who ask these questions are less interested in closures and more interested in higher order functions

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hey Michael, how is it going?

I think both are relevant.

Thanks for your comment.

enjoy the rest of your week!

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
gustavupp profile image
Gus Pear 🍐

What? Sh... That is awesome!
Thanks for including this post on your post @fruntend!

Collapse
 
carlosds profile image
Karel De Smet

Asking for an explanation and example of closure or hoisting are questions I would consider asking during an interview. But the other ones, not really.

Collapse
 
gustavupp profile image
Gus Pear 🍐

Hey Karel,

I agree.

Questions about closure and hoisting directly reflects the core understanding of the language, which is as you know of great importance.

The other questions about delete operator and having a function being evalueated inside an if statement are just tricky questions that are good to know and understand.

Thanks again for your comment.

Collapse
 
parthprajapati profile image
Parth

Nice one :) Earlier looked for some similar and easy to understood tut, but couldn't find it. Thanks

Collapse
 
gustavupp profile image
Gus Pear 🍐

Thans Parth, I am glad you liked it!

Have a great rest of your week

Collapse
 
gabrielprogramerx profile image
Gabrielprogramerx

I love JavaScript

Collapse
 
gustavupp profile image
Gus Pear 🍐

Me too! haha