## Ternary

The ternary operator makes for a shorthand if ... else

condition : if true do this ? else to this

For example the following if ... else:

var myWeight = 120;
var msg = "";
if(myWeight > 80){
msg = "big";
}else{
msg = "small";
}

… can be done with the ternary operator with:

var myWeight = 120;
var msg = (myWeight > 80) ? "big" : "small";

JSFIDDLE Demo

## Waltzing (m)tilde ~

The little used tilde character ~ is a bitwise operator that works right down on the bits – that is the zeros and ones.

The tilde reverses the order of the bits. The logic is ~N is -(N+1).

var myNum = 10;
myNewNum = ~myNum;
// myNewNum = -11

JSFIDDLE Demo

Running a variable through another tidle ie ~~ will switch the bits back to their original order.

JSFIDDLE Demo

But if you run a boolean false or true through a double tilde ~~ it has the effect of changing the value into an integer 0 or 1.

var myWeight = 120;
document.getElementById('display').innerHTML = ~~(myWeight > 80);

JSFIDDLE Demo

## Math with Modulus %

Modulus % will return the remainder of a division, and subsequently zero if the number is divisible by an integer.

12 % 5 = 2
10 % 5 = 0
104 % 100 = 4

Any number below the number to be divided will always return a value equal to itself. As the number will go into the number to be divided zero times and then what is left is the remainder. So:

1 % 10 = 1
2 % 10 = 2
7 % 10 = 7
8 % 10 = 8
9 % 10 = 9

… but

10 % 10 = 0

This is a useful way to keep track of whether an incrementing value has hit the top target value.

for(var i=0; i<15; i++){
if((i % 10) === 0){
document.getElementById('display').innerHTML = i;
}
}

JSFIDDLE Demo