The Least Significant Bit
Friday, September 23, 2016
JavaScript Puzzle: Questions With Interesting Answers
Questions
Question (1):
What is the value of
typeof(NaN)
?
Question (2):
If
08 === 8
and
09 === 9
are true, then, will
010 === 10
be true?
Question (3):
Again.. If
Infinity === Infinity
and
undefined === undefined
, will both evaluate to
true
, Will
NaN === NaN
be also true?
Question (4):
In many programming languages division by zero will throw an exception, but in JavaScript, what will be the value of
x
when
var x = 123 / 0;
?
Question (5):
You just learned that division by zero will evaluate to
Infinity
.. so you can tell what's the value of
x
when
var x = 0 / 0;
.. easy, right ?
Question (6):
If
arr
is an array, then
arr.length
will be increased or decreased after calling
arr.push
and
arr.pop
respectively. What is an easy way to make
arr.length
equals 50 ?
Question (7):
If
null <= 0
is true, does that mean
null < 0
or just means
null == 0
? (Hint:
null >= 0
is also true)
Question (8):
If this code will show an alert message
if ([]) { alert('hi'); }
, what's the output of this
if ([]
== true
) { alert('hi'); }
?
Question (9):
Which of the following will return true ?
isNaN(true);
isNaN(null);
isNaN(""); // empty string
isNaN(" "); // string with one or more spaces
Scroll for answers
Answers
Answer (1)
We all know that NaN is Not-A-Number, but some folks don't know that
typeof(NaN)
is
"number"
.
Answer (2)
No, 010 is not equal to 10, because JavaScript will deal with any number that starts with "0" and contains no digits greater that 7, as an octal (base 8) number.
Answer (3)
No, NaN is
not equal
to NaN. (However, we don't blame JavaScript for that, it's the
IEEE-754 spec for floating-point numbers)
.
Answer (4)
No, it will not be
undefined
, x will be
Infinity
.
Answer (5)
Exactly,
x
will be.. NaN.
Answer (6)
arr.length = 50;
in Javascript you can set the length of the array to expand or trim it.
Answer (7)
Neither, both of them is false, although null <= 0 is true. It's all about how the '<=' operator coerces its operands.
Answer (8)
No alert, The '==' operator coerces the value of an empty array to 'false'.
Answer (9)
All of them will return 'false'
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment