(+, -, *, /, %, ++, --, unary -, unary +) (=, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=) (&, |, ^, ~, <<, >>, >>>) (==, !=, ===, !==, >, >=, <, <=) (&&, ||, !) (+ and +=)Operators docs
Smooth Operator
Javascript has 17 levels of precedence
Are they useful in the real world?
-( n + 1 )
We can use for any functions that return -1
var foo = "baz",
checkFoo = foo.indexOf("bar");
// So instead of...
if ( checkFoo !== -1 ) {
}
// We can do
if ( ~checkFoo ) {
}
Next one will floor you!
>>>)Here's another useful bitwise operator.
A quick Math.floor for positive values (make sure it's positive tho)!
Array.prototype.forEach = function( fun, thisp ) {
var t, len;
if ( this === void 0 ||
this === null ||
typeof fun !== "function" ) {
throw new TypeError();
}
t = Object(this);
len = t.length >>> 0;
for ( var i = 0; i < len; i++ ) {
if ( i in t ) {
fun.call(thisp, t[i], i, t);
}
}
};
(condition ? ifTrue : ifFalse)
(,)
(function, delete, get, in, instanceof, let, new, set, this, typeof, void, yield)
Note: Technically, get and set are not really called anything in the spec, but are part of Property Descriptor defintions in Object Initialiser/PropertyAssignment. Mozilla Developer Network calls them operators. Either way, they're fun.
getBinds an object property to a function that will be called when that property is looked up.
var o = {
get latest () {
return this.log.length > 0 ?
this.log[ this.log.length - 1 ] :
null;
},
log: []
};
o.log[0] = "wowowow!";
o.latest === "wowowow!";
setBinds an object property to a function to be called when there is an attempt to set that property
var o = {
set current ( str ) {
return this.log[this.log.length] = str;
},
log: []
};
o.current = "foo";
o.log[0] === "foo";
delete o.current;
The downside
for (var i = 0, j = 9; i <= 9; i++, j--) {
document.writeln("a["+i+"]["+j+"] = " + a[i][j]);
}
var i, l, foo, bar, baz;
Use it when you want multiple expressions in a location that requires a single expression.
Detect IE in JS using conditional comments (No block in that while?!?)
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '',
all[0]
);
return v > 4 ? v : undef;
}());
== vs ===You know the difference. == attempts to coerce the values before comparison. So which should you use?
0 === 0 // true 0 == '0' // true 0 === '0' // false 0 == false // true 0 == '' // true null == undefined // true null === undefined // false
== wins for no type coercion and === wins with type coercion.=== whether type coercion is done or not.===, but type coercion significantly slows both.== is most often useful when doing comparison with null, also want to catch undefined.
function foo( arg ) {
if ( arg != null ) {
// Argument is present, but may be empty string or 0
}
}
function foo( arg ) {
if ( !!arg ) {
// Argument is valid
}
}
// Can be even shorter
function foo( arg ) {
if ( arg ) {
// 0, '', null, and undefined are "falsy"
}
}
=== may not be adequate."abc" == new String("abc") // true
"abc" === new String("abc") // false
== will check the value, but they are not the same type.
typeof "abc" // "string"
typeof new String("abc") // "object"
NaN == NaN // false typeof NaN === "number" // true var a = NaN; a == NaN || a === NaN // false isNaN( a ) // true, use this!
This is why we need isNaN()