Every value handled by a program has a data type. Types distinguish numbers, text, true-or-false values, and missing values. Like C, C++, and Java, JavaScript has distinct types, but JavaScript is dynamically typed: a variable is not permanently restricted to one type. Its current value determines the runtime type.
Common primitive types include number, string, boolean, undefined, and null. Arrays, functions, and ordinary objects belong to the object family. The typeof operator inspects a runtime type.
Variables
Use let for a variable that will be reassigned. Use const for a binding that will not be reassigned.
let score = 10;
console.log(score); // 10
score = 12;
const course = "JavaScript";
console.log(typeof course); // stringA const binding must be initialized and cannot be assigned again. This does not make every part of an object immutable; the arrays chapter returns to that distinction. Prefer const until reassignment is needed, then choose let. Avoid the older var declaration while learning because its scope rules are easier to misuse.
KNOWLEDGE QUIZ
Naming variables
A name may contain letters, digits, underscores, and $, but cannot start with a digit or use a reserved keyword. JavaScript is case-sensitive, so score and Score are different names. lowerCamelCase names such as studentName and totalScore are common.
KNOWLEDGE QUIZ
JavaScript keywords
Keywords are reserved for language structures and cannot become variable names. Common examples include let, const, if, else, switch, for, while, break, continue, function, return, class, try, and catch.
const userName = "Lin"; // valid
// const if = true; // invalid: if is a keywordKNOWLEDGE QUIZ