Data types and week 1 review
Every value in JavaScript belongs to a type, and the type decides what you can do with it: numbers are added, texts are joined, logical values are used to decide. Knowing the types prevents many strange errors. In this lesson we review the main primitive types and you learn to ask JavaScript what type a value has using the typeof operator.
The primitive types
- number: integers and decimals, like 42 or 3.14. It includes special values: Infinity and NaN (not a number).
- string: text between single quotes, double quotes or backticks, like 'hello'.
- boolean: only true or false.
- undefined: a declared variable that has no value yet.
- null: an absence of value put there on purpose by the programmer.
- bigint: huge integers, written with an n at the end, like 9007199254740993n.
- symbol: unique identifiers; they are rarely used when starting.
const age = 30;
const price = 19.99;
const name = 'Ana';
const isStudent = true;
let nothingYet;
const empty = null;
console.log(typeof age); // number
console.log(typeof price); // number
console.log(typeof name); // string
console.log(typeof isStudent); // boolean
console.log(typeof nothingYet); // undefined
console.log(typeof empty); // object (a historical bug in the language)
console.log(typeof 10n); // bigintOddities you should know
First, typeof null returns 'object'. It is a historical bug that was never fixed so as not to break old pages; to check for null use value === null. Second, NaN means a numeric operation made no sense, for example 'abc' * 2, and curiously typeof NaN is 'number'. Third, decimals are stored in binary, so 0.1 + 0.2 gives 0.30000000000000004. It is not a JavaScript flaw, it happens in almost every language; for money you work with whole cents.
console.log(0.1 + 0.2); // 0.30000000000000004
console.log((0.1 + 0.2).toFixed(2)); // '0.30' (a string)
console.log('abc' * 2); // NaN
console.log(Number.isNaN('abc' * 2)); // true
console.log(10 / 0); // Infinity
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991Primitives and objects
Everything that is not a primitive is an object: arrays, functions, dates and the objects you will create in week 6. Primitives are immutable, they cannot be modified internally; when you do an operation you get a new value. Objects, on the other hand, are stored by reference. You will see this in detail later, but it is good to know from now that there are two families of values.
Week review: typical mistakes
Before the quiz, review the essentials of the week. Running code is done in the console or with node file.js. Values are stored in variables: const by default and let if they change. Every value has a type and typeof tells you which one, with the exception of null. The most common failures are confusing a text with a number, forgetting that capitalization matters, expecting exact decimals and reassigning a const. If you make any of these mistakes, it is fine: the error message is a clue, not a punishment.
Teste dein Wissen
Frage 1 von 6Which keyword do you use to declare a value that will not be reassigned?
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen