There are six basic types of values in JavaScript numbers, strings, Booleans, objects, functions, and undefined values.
**Number : > 粗体 Notice 2.998e8 ** scientific notation “e” (for “exponent”) 2.998 × 10^8 = 299,800,000.
print == console.log(typeof "x")
JavaScript has a Boolean type, which has just two values: true and false
0 == false and "" == false are also true === and !==.
The first tests whether a value is precisely equal to the other. the second tests whether it is not precisely equal.
Logical operators
console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true
Function
- alert("Good morning!");
- The console.log function
console.log("the value of x is", x);
// → the value of x is 30
- prompt and confirm confirm("Shall we, then?"); . This returns a Boolean: true if the user clicks OK false if the user clicks Cancel.
Control flow
if (num < 10)
alert("Small");
else if (num < 100)
alert("Medium");
else
alert("Large");
Defining a function
var square = function(x) {
return x * x;
};
console.log(square(12));
// → 144
The function body must always be wrapped in braces, even when it consists of only a single statement
js array vs python list
push == append
var mack = [];
mack.push("Mack");
mack.push("the", "Knife");
console.log(mack);
// → ["Mack", "the", "Knife"]
console.log(mack.join(" "));
// → Mack the Knife
console.log(mack.pop());
// → Knife