# JavaScript expressions and operators

Posted by harshilshah on Tue, 08 Feb 2022 11:15:56 +0100

The addition assignment operator (+ =) adds the value of the right operand to the variable and assigns the result to the variable. The type of the two operands determines the behavior of the addition assignment operator. Addition or concatenation is possible.

```let a = 2;
let b = 'hello';

// expected output: 5

console.log(b += ' world'); // concatenation
// expected output: "hello world"
```

The addition operator (+) is used to add two operands. If the operand is a string, the operator connects the two operands into a string.

```console.log(2 + 2);
// expected result: 4

console.log(2 + true);
// expected result: 3

console.log('hello ' + 'everyone');
// expected result: "hello everyone"

console.log(2001 + ': A Space Odyssey');
// expected result: "2001: A Space Odyssey"
```

## Assignment operator (=)

The simple assignment operator (=) is used to assign values to variables. The value of the assignment expression itself is the value of the variable assigned after its completion. To assign a value to multiple variables, you can use the assignment operator chained.

```let x = 2;
const y = 3;

console.log(x);
// expected output: 2

console.log(x = y + 1); // 3 + 1
// expected output: 4

console.log(x = x * y); // 4 * 3
// expected output: 12
```

## Bitwise and assignment (& =)

The bitwise AND assignment operators (& =) represent the binary of two operands, bitwise AND them AND assign the results to variables.

### example

```let a = 5;      // 00000000000000000000000000000101
a &= 3;         // 00000000000000000000000000000011

console.log(a); // 00000000000000000000000000000001
// expected output: 1
```

### grammar

```Operator: x &= y
Meaning:  x  = x & y
```

## Bitwise and (&)

The bitwise and operator (&) returns 1 on each bit, and the corresponding bits of both operands are 1

```a & b
```

### describe

The operand is converted to a 32-bit integer and represented by a series of bits (0 and 1). Numbers exceeding 32 bits will discard their most significant bits. For example, the following integers larger than 32 bits will be converted to 32-bit integers:

```Before: 11100110111110100000000000000110000000000001
After:              10100000000000000110000000000001
```

Each bit in the first operand is paired with the corresponding bit in the second operand: first to first, second to second, and so on.

Apply an operator to each pair of bits and construct the result bitwise.

Truth table of and operation:

aba AND b
000
010
100
111
```.    9 (base 10) = 00000000000000000000000000001001 (base 2)
14 (base 10) = 00000000000000000000000000001110 (base 2)
--------------------------------
14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
```

Bitwise summing any number x with 0 yields 0.

### Examples

Using bitwise AND

```// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
5 & 2; // 0
```

## Bitwise non (~)

Bitwise non operator (~), inverts the bits of the operand.

```~a
```

### describe

Operands are converted to 32-bit binary representations (0 and 1). Numbers exceeding 32 bits will discard their most significant bits. In the following example, integers exceeding 32 bits are converted to 32-bit integers:

```Before: 11100110111110100000000000000110000000000001
After:              10100000000000000110000000000001
```

Each bit in the first operand is paired with the corresponding bit in the second operand: first to first, second to second, and so on.

Apply an operator to each pair of bits and construct the result bitwise.

Truth table of non operation:

aNOT a
01
10
``` 9 (base 10) = 00000000000000000000000000001001 (base 2)
--------------------------------
~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
```

In bitwise non operation, the operation result of any number x is ` ` - (x + 1). For example, the operation result of ~ - 5 is 4.

Note that due to using 32-bit representation for numbers both ~-1 and ~4294967295 (232-1) results in 0.

Note that because of the 32-bit representation of the numbers ~ - 1 and ~ 4294967295 (232-1), the result is 0.

### example

Use bitwise inversion

```~0;  // -1
~-1; // 0
~1;  // -2
```

Reference documents MDN

Topics: Javascript html