I use javascript:void(0)
.
Three reasons. Encouraging the use of #
amongst a team of developers inevitably leads to some using the return value of the function called like this:
function doSomething() {
//Some code
return false;
}
But then they forget to use return doSomething()
in the onclick and just use doSomething()
.
A second reason for avoiding #
is that the final return false;
will not execute if the called function throws an error. Hence the developers have to also remember to handle any error appropriately in the called function.
A third reason is that there are cases where the onclick
event property is assigned dynamically. I prefer to be able to call a function or assign it dynamically without having to code the function specifically for one method of attachment or another. Hence my onclick
(or on anything) in HTML markup look like this:
onclick="someFunc.call(this)"
OR
onclick="someFunc.apply(this, arguments)"
Using javascript:void(0)
avoids all of the above headaches, and I haven't found any examples of a downside.
So if you're a lone developer then you can clearly make your own choice, but if you work as a team you have to either state:
Use href="#"
, make sure onclick
always contains return false;
at the end, that any called function does not throw an error and if you attach a function dynamically to the onclick
property make sure that as well as not throwing an error it returns false
.
OR
Use href="javascript:void(0)"
The second is clearly much easier to communicate.
The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.
Using the other type of quote as a literal:
alert('Say "Hello"');
alert("Say 'Hello'");
This can get complicated:
alert("It's \"game\" time.");
alert('It\'s "game" time.');
Another option, new in ECMAScript 6, is template literals which use the backtick character:
alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);
Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.
Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.
Best Answer
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 same type===
will simply returnfalse
. Both are equally quick.To quote Douglas Crockford's excellent JavaScript: The Good Parts,
Update:
A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning objects. For objects,
==
and===
act consistently with one another (except in a special case).The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its
toString
orvalueOf
method. For example, consider the comparison of a string primitive with a string object created using theString
constructor.Here the
==
operator is checking the values of the two objects and returningtrue
, but the===
is seeing that they're not the same type and returningfalse
. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use theString
constructor to create string objects from string literals.Reference
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3