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 Type Conversions
The addition operator (+) doesn't convert strings to numbers "2" + "3" # 23, not 5 "6" / "3" # 2
Numeric
Most of the time Javascript automatically converts to the right type.
/**
* Mathematical functions and expressions ...
* strings are automatically converted to numbers.
*
* The addition operator (+) doesn't convert strings to numbers.
*/
console.log("2" + "3"); // 23, not 5
console.log("6" / "3"); // 2
console.log(Number("2") + Number("3")); // 5
console.log(String(2) + String(3)); // 23 - Look Here
Boolean
In Javascript, a non-empty string is always true.
/**
* In Javascript "0" is not threated as false (like in PHP)
*/
let a = "0";
if (a) {
console.log("hello"); // hello
}
➥ Questions