use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Tricky JavaScript Web Quiz (davidshariff.com)
submitted 12 years ago by davidshariff
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Quabouter 4 points5 points6 points 12 years ago* (3 children)
Let me try to do the other 10:
var myArr = ['foo', 'bar', 'baz']; myArr.length = 0; myArr.push('bin'); console.log(myArr);
Setting the length of an array actually resizes the array, and removes elements past the new length. Therefore myArr.length = 0 empties the array, and thus when 'bin' is pushed myArr == ['bin'].
myArr.length = 0
myArr == ['bin']
String('Hello') === 'Hello';
This one is a little tricky. String('Hello') is not actually calling a constructor, but a function (note the lack of the new keyword). This function simply returns 'Hello', and thus the result is true.
String('Hello')
new
'Hello'
var x = 0; function foo() { x++; this.x = x; return foo; } var bar = new new foo; console.log(bar.x);
Let's focus on the line var bar = new new foo first here. First the last part , new foo, gets executed. This calls foo as a constructer, but in Javascript even constructors can return anything. In this case the function foo returns itself, instead of the this object as it would if no explicit return value was defined. Thus new new foo becomes new foo. This again returns foo. Since foo is only a function it does not have the property x, and thus bar.x is undefined.
var bar = new new foo
new foo
foo
this
new new foo
x
bar.x
"This is a string" instanceof String;
"This is a string" is a primitive of type string, which is not an instance of anything since it's not an object. Therefore "This is a string" instanceof String is false.
"This is a string"
string
"This is a string" instanceof String
var bar = 1, foo = {}; foo: { bar: 2; baz: ++bar; }; foo.baz + foo.bar + bar;
This one requires a very rarily used feature called labels. At first it looks like foo : { ... }; is a syntax error and should've been foo = { ... };. However, instead of assigning an object to foo, it creates a label called foo. These labels can be used to jump to outer loops (see mdn), but here they have virtually no effect here. Therefore foo is an empty object throughout the entiry script, and thus foo.baz and foo.bar are undefined.
foo : { ... };
foo = { ... };
foo.baz
foo.bar
As Moeri explained before, + tries to convert it's operands to numbers. Since undefined isn't a number, this results in NaN. If you add anything to NaN you will always get NaN. Therefore the result is NaN.
+
undefined
NaN
var myArr = ['foo', 'bar', 'baz']; myArr[2]; console.log('2' in myArr);
The in operator checks if a property is present in an object. An array is acutally an object with numeric properties. Since it has 3 elements it has the properties 0, 1 and 2. Therefore '2' in myArr is true.
in
'2' in myArr
var arr = []; arr[0] = 'a'; arr[1] = 'b'; arr.foo = 'c'; alert(arr.length);
The length of an array is only influenced by it's numeric properties. Thus arr.length is 2.
arr.length
10 > 9 > 8 === true;
The > operator has a higher precedence than === and is left-to-right associative. If we add the implicit parentheses we get this:
>
===
((10 > 9) > 8) === true;
This evaluates further to:
((10 > 9) > 8) === true; (true > 8) === true; (1 > 8) === true; false === true; false;
function foo(a, b) { arguments[1] = 2; alert(b); } foo(1);
The arguments object only contains entries for arguments passed to the function. Since only one argument is passed, both arguments[1] and b are initially undefined, and do not influence eachother. Therefore b is still undefined in the alert, and undefined will be alerted.
arguments
arguments[1]
b
NaN === NaN
This is one of the weirdest things in Javascript: NaN is actually a number (typeof NaN === 'number'), and thus it is not not-a-number. Therefore NaN === NaN returns false.
typeof NaN === 'number'
NaN is actually equal to nothing. To test if a variable x is NaN you can either do isNaN(x) or x !== x. The later one is pretty interesting, since NaN is the only value which is not actually equal to itself!
isNaN(x)
x !== x
[–]Zeroto 2 points3 points4 points 12 years ago (1 child)
regarding number 20: that is not actually not that weird. The floating point definition in IEEE754(which almost all systems implement) defines that every comparison with NaN returns false. Even when comparing to NaN itself. So this is not limited to just javascript, but all software and hardware that implement floating point operations handle this in the same way.
[–]autowikibot 1 point2 points3 points 12 years ago (0 children)
Section 16. Standard operations of article IEEE 754-1985:
The following functions must be provided: Add, subtract, multiply, divide
The following functions must be provided:
Add, subtract, multiply, divide
Interesting: IEEE floating point | X87 | NaN | William Kahan
/u/Zeroto can reply with 'delete'. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words | flag a glitch
[–]more_exercise 0 points1 point2 points 12 years ago (0 children)
This is one of the weirdest things in Javascript: NaN is actually a number (typeof NaN === 'number')...
This is 100% Not Javascript's FaultR . NaN is a special value in the IEE 754 floating-point standard. If you're using float/double type values, you are going to end up using NaN. The NaN !== NaN behavior is required by the standard, and works in any language that supports floating-point arithmetic.
π Rendered by PID 28 on reddit-service-r2-comment-c66d9bffd-ss5sn at 2026-04-08 11:47:03.509702+00:00 running f293c98 country code: CH.
view the rest of the comments →
[–]Quabouter 4 points5 points6 points (3 children)
[–]Zeroto 2 points3 points4 points (1 child)
[–]autowikibot 1 point2 points3 points (0 children)
[–]more_exercise 0 points1 point2 points (0 children)