Which equals operator (== vs ===) should be used in JavaScript comparisons?

The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal. Reference: Javascript Tutorial: Comparison Operators The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the … Read more

How do you use the ? : (conditional) operator in JavaScript?

This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: This can be shortened with the ?: like so: Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of … Read more

What is TypeScript and why would I use it in place of JavaScript? [closed]

I originally wrote this answer when TypeScript was still hot-off-the-presses. Five years later, this is an OK overview, but look at Lodewijk’s answer below for more depth 1000ft view… TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment … Read more

For-each over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) a simple old-fashioned for loop – async-friendly (rarely) for-in with safeguards – async-friendly Some quick “don’t”s: Don’t use for-in … Read more

What does “javascript:void(0)” mean?

The void operator evaluates the given expression and then returns undefined. The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value). An … Read more

What’s the difference between “{}” and “[]” while declaring a JavaScript array?

Nobody seems to be explaining the difference between an array and an object. [] is declaring an array. {} is declaring an object. An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in … Read more

How can I generate a rainbow circle using HTML5 canvas?

This is not perfect (due to drawing steps …), but it can help you : http://jsfiddle.net/afkLY/2/ HTML: Javascript: The idea is to draw the disc line by line with a hue value corresponding to the line direction. You can change the color base rotation by adding a radius angle to rad variable (adding -pi/2 to rad would make the … Read more