DEV Community

loading...
Cover image for JavaScript Interview Question #31: Big or small

JavaScript Interview Question #31: Big or small

Coderslang: Become a Software Engineer
Teaching you to code at js.coderslang.com - JavaScript, HTML, CSS, Node.js, React.js, React Native
Originally published at learn.coderslang.com on ・1 min read

js-test-31

What's the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

So, there’s an if statement and it’s condition Math.max() > 0 is all we need to analyze.

If your first guess was that Math.max() should return some big number that’s for sure bigger than 0, then you’re wrong.

In JavaScript Math.max() takes a variable number of arguments and returns the biggest one. The comparison starts at the very bottom, which in JS is -Infinity because it’s smaller than all other numbers.

This is why if no arguments are provided to the Math.max(), it will return -Infinity.

As -Infinity is smaller than 0, we’ll go into the else branch of the conditional statement.


ANSWER: string ZERO! will be logged to the console.

Learn Full-Stack JavaScript

Discussion (2)

Collapse
raddevus profile image
raddevus

Yeah, I recently wrote up a very short article here on Dev.to about this (Esoteric Questions During Front-end Interview: Valid?) Questions you should ask your interviewer if they ask you an esoteric tech question.