Summary - Variables and types
Combine variable declaration and data types to write a small profile card.
Summary - Variables and types
In this unit you learned to declare variables in JavaScript. With let you declare changing values, with const fixed values; var is old style. You print values to the screen with console.log.
You met the three core primitive types: number, string and boolean. null is an intentional empty value, undefined is an unassigned value. You checked types with typeof and saw the typeof null "object" pitfall.
Exercise Write a profile card
Declare three variables: ad (a name, string), yas (an age, number), aktif (is the user active, boolean). Then print all three together with console.log.
Show example solution
const ad = "Alex";
const yas = 28;
const aktif = true;
console.log(ad, yas, aktif);
Exercise Print a variable's type with typeof
Declare a variable (example: a city name). Then find its type with typeof and print it with console.log.
Show example solution
let sehir = "Berlin";
console.log(typeof sehir);
Test yourself
-
Which operator finds a variable's type?
- type
- typeof
- kindof
- dtype
Show answer
typeof — Correct. The typeof operator returns a value's type as a string.
-
Match concepts to their descriptions
- let
- Value can change
- const
- Cannot be reassigned
- null
- Intentional empty
- typeof
- Tells the type