# Some interesting operators and expressions in Javascript

**Unary negation (-)**

Returns the negation of its operand.

Example:

If x is 3, then -x returns -3.

**Unary plus (+)**

Attempts to convert the operand to a number, if it is not already.

Examples:

a) +”3" returns 3.

b) +true returns 1.

**Exponentiation operator (**)**

Calculates the `base`

to the `exponent`

power.

Examples:

a) 2 ** 3 returns 8.

b) 10 ** -1 returns 0.1.

**Bitwise operators (&-AND,|-OR,^-XOR)**

Treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers.

Ex: 9 has a binary representation of 1001.

Bitwise operators perform their operations on such binary representations and return numerical values.

Ex: 5 & 13 is 5

0101 & 1101 is 0101

# << Left shift

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

For example, `9 << 2`

yields 36:

`9 (base 10): 00000000000000000000000000001001 (base 2)`

--------------------------------

9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)

Bitwise shifting any number `x`

to the left by `y`

bits yields `x * 2 ** y`

.

# >> Sign-propagating right shift

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name “sign-propagating”.

For example, `9 >> 2`

yields 2:

`9 (base 10): 00000000000000000000000000001001 (base 2)`

--------------------------------

9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)

Likewise, `-9 >> 2`

yields `-3`

, because the sign is preserved:

`-9 (base 10): 11111111111111111111111111110111 (base 2)`

--------------------------------

-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)

# >>> Zero-fill right shift

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative.

For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, `9 >>> 2`

yields 2, the same as `9 >> 2`

:

`9 (base 10): 00000000000000000000000000001001 (base 2)`

--------------------------------

9 >>> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)

However, this is not the case for negative numbers. For example, `-9 >>> 2`

yields 1073741821, which is different than `-9 >> 2`

(which yields `-3`

):

`-9 (base 10): 11111111111111111111111111110111 (base 2)`

--------------------------------

-9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)

**function* expression**

A Good blog: https://codeburst.io/understanding-generators-in-es6-javascript-with-examples-6728834016d5

Used to define a generator function inside an expression.A generator is a function that **can stop midway** and then continue *from where it stopped.It *is a function that produces a sequence of results instead of a single value, i.e you *generate *a series of values.

Ex: 1

function* foo() {

yield ‘a’;

yield ‘b’;

yield ‘c’;

}

How to use this foo?

var str = “”;

for (let val of foo()) {

str = str + val;

}

console.log(str);

// expected output: “abc”

Ex:2

function *generatorFunction() {

console.log('This will be executed first.');

yield'Hello, '; // Line 2console.log('I will be printed after the pause');

yield'World!';

}constgeneratorObject= generatorFunction();console.log(generatorObject.next().value);

console.log(generatorObject.next().value);

console.log(generatorObject.next().value);// This will be executed first.

// Hello,

// I will be printed after the pause

// World!

// undefined

Note:

In Line 6, we again invoke `next()`

. This time there are no more lines to execute. Remember that every function implicitly returns `undefined`

if no return statement is provided. Hence, the generator returns (instead of yielding) an object `{ value: undefined, done: true}`

. The `done`

is set to `true`

. This signals the end of this generator. Now, it can’t generate more values or resume again since there are no more statements to be executed.

We’ll need to make new another generator object to run the generator again.

**Void operator**

Evaluates the given expression and then returns **undefined**.

Ex:

void function test() {

console.log(‘boo!’);

// expected output: “boo!”

}();try {

test();

}

catch(e) {

console.log(e);

// expected output:ReferenceError: test is not defined

}

Even though we have defined a function test(),because of void operator it turns out to be an expression and returns undefined.