Javascript
/
Fundamentals
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
Javascript Fundamentals Maths
Exponentiation operator, raises to power The unary operator (+) converts string to number 2 ** 3 #8 2 + +"3" #5Bitwise
Exponentiation
The exponentiation operator a ** b raises a to the power of b.
/**
* The exponentiation operator a ** b raises a to the power of b
* Power of 1/2 is the same as square root
* Power of 1/3 is the same as cubic root
*/
console.log(2 ** 3); // 8
console.log(4 ** 1/2); // 2
console.log(8 ** 1/3); // 2
console.log(2 ^ 3); // 1 - XOR bitwise
console.log(2 ^ 2); // 0
console.log(3 % 2); // 1 - reminder
console.log(8 % 3); // 2
Unary +
If the operand is not a number, the unary plus converts it into a number.
/**
* The unary operator (+) applied to a single value
* It doesn’t do anything to numbers
* It converts the string to number
*
* Unary pluses applied to values before the binary ones ...
* because of their higher precedence
*/
let a = 1;
let b = true;
let x = "2";
let y = "3";
console.log( +a ); // 1
console.log( +b ); // 1
console.log( +x + +y ); // 5
➥ Questions