Basic data types
suggest changeNumbers
Numbers are 8 byte, floating-point.
const i = 16;
const j = 38.53;
const z = i + j;
console.log(z); // -> 54.53
54.53
Strings
Strings are Unicode.
let s = "my";
s = s + " message";
console.log(s); // -> my message
my message
Arrays
Arrays can grow and shrink and elements can be of any type.
const cars = ["Model 3", "Leaf", "Bolt"];
console.log("first car:", cars[0]);
const mixedTypes = ["string"];
mixedTypes.push(5);
console.log("array with mixed types:", mixedTypes);
first car: Model 3
array with mixed types: [ 'string', 5 ]
Objects (dictionaries, hash tables)
Objects map keys to values. In other languages they are known as dictionaries or hash tables. Values can be of any type, including functions.
const person = {
firstName: "John",
lastName: "Doe",
log: () => console.log("hello from log()")
};
console.log("firstName:", person.firstName);
console.log("lastName:", person["lastName"]);
person.log();
firstName: John
lastName: Doe
hello from log()
Functions
Functions are first-class meaning they can be assigned to variables and passed around.
const logMe = function(s) {
console.log(s);
}
const logMe2 = logMe;
logMe2("hello");
hello
Dynamic typing
The same variable can be assigned values of different types.
let v;
console.log(v);
v = 5;
console.log(v);
v = "str";
console.log(v);
undefined
5
str
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents