Back
Chapter 1.2 JS Datatypes
Pratyush M2022-09-15
We will be covering 3 topics in this chapter
Chapter 1.2 - Data types
- Primitive Values v/s Objects
- Undefined and Null
- Type of Results
Primitive Values Vs Objects
- Primitive types in JavaScript are of 5 types
- strings
- numbers (Number and BigInt)
- booleans (true or false)
- undefined
- Symbol values
null
is a special primitive type. If you run typeof null
you’ll get object
back, but it’s actually a primitive type.
-
Everything that is not a primitive type is an Object.
-
A major difference Between the two is how they are compared, as each object has a unique identity and is only (strictly) equal to itself. we can se a code explaining the same below
var object1 = {}; // empty obj 1
var object2 = {}; // empty obj 2
console.log(object1 === object2); // false
console.log(object1 === object1); // true
Undefined and Null
Most programming languages have values denoting missing information.
JavaScript has two such “nonvalues” : undefined
and null
undefined
means “no value.”
- Uninitialized variables are
undefined
:
var x;
console.log(x); // undefined
- Missing parameters are
undefined
function sum(a, b) {
console.log(a); // undefined
console.log(b); // undefined
}
sum(); // point of execution
function sum(a, b) {
console.log(a); // 5
console.log(b); // undefined
}
sum(5); // point of execution
function sum(a, b) {
console.log(a); // 5
console.log(b); // 6
}
sum(5, 6); // point of execution
- If you read a nonexistent property, you get
undefined
const obj = {};
console.log(obj.firstName); //undefined
null
means “no object” . It is used as a nonvalue whenever a object is expected (parameters ,last i a chain of objects etc.)
function countLength(obj) {
return Object.keys(obj).length;
}
console.log(countLength({ a: 5 })); // 1
when we dont have a object
function countLength(obj) {
if (obj === null) {
throw new Error("Expected an object"); // Uncaught Error: Expected an object"
}
return Object.keys(obj).length;
}
console.log(countLength(null));
Type of Results
typeof
results
Operand | Result |
---|---|
undefined |
undefined |
null |
object |
true , false |
boolean |
0 ,1 , 2 ,.... |
number |
"Hello World" |
string |
function countLength(obj) { // statements } |
function |
All other normal values | object |
Like my blogs ?
You can give this github repo a ⭐Here