A primitive
Is a value of a primitive type.
There are 7 primitive types: string, number, bigint, boolean, symbol, null and undefined.
An object
Is capable of storing multiple values as properties.
Can be created with {}, for instance:
There are other kinds of objects in JavaScript: functions, for example, are objects.
Is a value of a primitive type.
There are 7 primitive types: string, number, bigint, boolean, symbol, null and undefined.
An object
Is capable of storing multiple values as properties.
Can be created with {}, for instance:
{name: "John", age: 30}.There are other kinds of objects in JavaScript: functions, for example, are objects.
let john = {
name: "John",
sayHi: function() {
alert("Hi buddy!");
}
};
john.sayHi(); // Hi buddy!
One of the best things about objects is that we can store a function as one of its properties.Common binary operators:
Arithmetic: +, -, *, /, %
Assignment: =, +=, -=, *=, /=
Comparison: ==, !=, ===, !==, >, <, >=, <=
Logical: &&, ||
Bitwise: &, |, ^, <<, >>, >>>
Ternary: condition ? expr1 : expr2 (Special case, but still binary because of two expressions)
Arithmetic: +, -, *, /, %
Assignment: =, +=, -=, *=, /=
Comparison: ==, !=, ===, !==, >, <, >=, <=
Logical: &&, ||
Bitwise: &, |, ^, <<, >>, >>>
Ternary: condition ? expr1 : expr2 (Special case, but still binary because of two expressions)
Common unary operators:
+ (Unary plus, converts to number)
- (Unary negation, negates a number)
! (Logical NOT)
typeof (Returns type of a variable)
delete (Deletes object properties)
void (Evaluates an expression but returns undefined)
++ (Increment)
-- (Decrement)
+ (Unary plus, converts to number)
- (Unary negation, negates a number)
! (Logical NOT)
typeof (Returns type of a variable)
delete (Deletes object properties)
void (Evaluates an expression but returns undefined)
++ (Increment)
-- (Decrement)
A Set is a special type collection – “set of values” (without keys), where each value may occur only once.
Its main methods are:
new Set([iterable]) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
set.add(value) – adds a value, returns the set itself.
set.delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set.has(value) – returns true if the value exists in the set, otherwise false.
set.clear() – removes everything from the set.
set.size – is the elements count.
Its main methods are:
new Set([iterable]) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
set.add(value) – adds a value, returns the set itself.
set.delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set.has(value) – returns true if the value exists in the set, otherwise false.
set.clear() – removes everything from the set.
set.size – is the elements count.
let set = new Set();
let john = { name: "John" };
let pete = { name: "Pete" };
let mary = { name: "Mary" };
// visits, some users come multiple times
set.add(john);
set.add(pete);
set.add(mary);
set.add(john);
set.add(mary);
// set keeps only unique values
alert( set.size ); // 3
for (let user of set) {
alert(user.name); // John (then Pete and Mary)
}
let user = {
name: "John",
age: 30,
toString() {
return `{name: "${this.name}", age: ${this.age}}`;
}
};
alert(user); // {name: "John", age: 30}