An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
May 24, 2021 03:20 pm GMT
Original Link: https://dev.to/coderslang/javascript-interview-question-42-how-math-max-works-in-js-13ei
JavaScript Interview Question 42: How Math.max works in JS
How exactly Math.max works in JavaScript? Whats the output?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
In JavaScript, the function Math.max()
accepts variable number of arguments and returns the biggest of them.
If you pass a couple of arrays into Math.max
they will be first converted to strings and then into numbers:
console.log(Math.max([ 0 ], [ 1 ])); // 1console.log(Math.max("0", "1")); // 1console.log(Math.max(0, 1)); // 1
Booleans will be also converted to numbers. true
becomes one and false
becomes zero:
console.log(Math.max(true, false)); // 1console.log(Math.max(0, 1)); // 1
Now the condition inside of an if
statement can be simplified and we can make sure were getting into the else
branch:
if (1 > 1) { // false console.log('array won');} else { console.log('array lost');}
ANSWER: The string array lost
will be logged to the console.
Read more JavaScript Tutorials or Learn Full-Stack JavaScript
Original Link: https://dev.to/coderslang/javascript-interview-question-42-how-math-max-works-in-js-13ei
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To