Basic data types
suggest changeNumbers
The Number type is a 64-bit IEEE 754 floating-point value (8 bytes), not a separate integer type. BigInt is a different type and is not covered here.
// :glot
const i = 16;
const j = 38.53;
const z = i + j;
console.log(z); // -> 54.53
54.53
Strings
Strings are sequences of UTF-16 code units. A single Unicode code point may take one or two units.
// :glot
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.
// :glot
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.
// :glot
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