Javascript
/
Fundamentals
- 1 Fundamentals 11
-
Hello World
-
Code structure
-
Use strict
-
Variables
-
Data types
-
Type conversions
-
Maths
-
Comparitions
-
Conditional
-
Loops
-
Functions
- 2 Testing 2
-
Mocha
-
Nested describe
- 3 Objects 4
-
Basics
-
Reference
-
Methods
-
Constructor
- 4 Types 5
-
Primitives
-
Numbers
-
Strings
-
Arrays
-
Json
- 5 Classes 3
-
Constructor
-
Binding
-
Inheritance
/
Maths
➟
➟
Last update: 25-01-2022
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.
* A in maths, square root is an exponentiation by 1/2
*/
console.log(2 ** 3); // 8
console.log(4 ** 1/2); // 2 (power of 1/2 is the same as square root)
console.log(8 ** 1/3); // 2 (power of 1/3 is the same as cubic root)
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 plus operator + applied to a single value, ...
* doesn’t do anything to numbers.
*
* The need to convert strings to numbers arises very often.
*/
let a = 1;
console.log( +a ); // 1 - no effect on numbers
let flag = true;
console.log( +flag ); // 1 - convertion
let b = 0;
console.log( +b ); // 0 - convertion
let c = "2";
let d = "3";
console.log( +c + +d ); // 5
Precendence
Unary pluses applies before the binary pluses.
/**
* Unary pluses applied to values before the binary ones ...
* because of their higher precedence.
*
* + // unary
* ** // exponentiation
* * // multiplication
* / // division
* + // addition
* - // subtraction
* = // assignment
*
* Parentheses override any precedence.
*/
let u = +"1" + +"2";
console.log(u); // 3
let a = 1 + 2 * 3;
let b = (1 + 2) * 3;
console.log(a); // 7
console.log(b); // 9
➥ Questions github Fundamentals