based on this course
Numbers and strings are values. Objects and functions are values, too. Our code interacts with values, but values exist in a completely separate space. My code contains instructions like “make a function call,” “do this thing many times,” or even “throw an error.” I might refer to values in my code, but they don’t exist inside my code.
- Primitive Values. They can be numbers and strings, among other things
- Undefined (undefined), used for unintentionally missing values.
- Null (null), used for intentionally missing values.
- Booleans (true and false), used for logical operations.
- Numbers (-100, 3.14, and others), used for math calculations.
- BigInts (uncommon and new), used for math on big numbers.
- Strings ("hello", "abracadabra", and others), used for text.
- Symbols (uncommon), used to perform rituals and hide secrets.
console.log(2);
console.log("hello");
console.log(undefined);
- Objects and Functions. Objects and functions are also values but, unlike primitive values, I can manipulate them from my code. Functions are objects ;)
- Objects ({} and others), used to group related data and code.
- Functions (x => x * 2 and others), used to refer to code.
console.log({});
console.log([]);
console.log(x => x * 2);
Note: In JavaScript, there are no other fundamental value types other than the ones we have just enumerated. The rest are all objects!
If we want to check a value’s type, we can ask with the typeof
operator.
console.log(typeof(2)); // "number"
console.log(typeof("hello")); // "string"
console.log(typeof(undefined)); // "undefined"
There are some questions that JavaScript would be delighted to answer. These questions have a special name—they are called expressions. If we “ask” the expression 2 + 2
, JavaScript will “answer” with the value 4
.
JavaScript answers expressions in the only way it knows how—with values. Expressions always result in a single value.