JS Tutorials
JS Objects
JS Functions
JS Classes
JS Async
In JavaScript, objects are king. If you understand objects, you understand JavaScript.
In JavaScript, almost "everything" is an object.
new
keyword)new
keyword)new
keyword)All JavaScript values, except primitives, are objects.
A primitive value is a value that has no properties or methods.
A primitive data type is data that has a primitive value.
JavaScript defines 5 types of primitive data types:
string
number
boolean
null
undefined
Primitive values are immutable (they are hardcoded and therefore cannot be changed).
if x = 3.14, you can change the value of x. But you cannot change the value of 3.14.
Value | Type | Comment |
---|---|---|
"Hello" | string | "Hello" is always "Hello" |
3.14 | number | 3.14 is always 3.14 |
true | boolean | true is always true |
false | boolean | false is always false |
null | null (object) | null is always null |
undefined | undefined | undefined is always undefined |
JavaScript variables can contain single values:
let person = "John Doe";
JavaScript variables can also contain many values.
Objects are variables too. But objects can contain many values.
Object values are written as name : value pairs (name and value separated by a colon).
let person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
A JavaScript object is a collection of named values
It is a common practice to declare objects with the const
keyword.
const person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};