- Everything in JavaScript is an Object
- Just like object in anyother language, it has its own properties and methods
- We can check the list of methods available for an object in the proto section of the object
- “window” is a built in object of the browser, it the mother of all object on the browser.
- “window” object is the object of the browser in which you are executing the command
Using browser console to perform object creation :
- In above example we have created an array object
- As we type the object name “names” in the console we could expand it and see the properties in that object, in our case length is the property of that object
- The proto section in the above screenshot displays the methods available for that object.
Calling a property of an object
Calling a method on an Object
Example 2 : For Objects.
Data Types :
- There are 6 primitive data types: string, number, bigint, boolean, undefined, and symbol.
- There also is null, which is seemingly primitive, but indeed is a special case
- Most of the time, a primitive value is represented directly at the lowest level of the language implementation.
- All primitives are immutable, i.e., they cannot be altered.
- It is important not to confuse a primitive itself with a variable assigned a primitive value.
- The variable may be reassigned a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.
- Since primitive data types are not object technically you shouldn’t be able to run methods on those but JavaScript is a bit smart i.e it wraps the Primitive data types into an object when you going to execute some methods on primitive data types
Example :
// Using a string method doesn't mutate the string var bar = "baz"; console.log(bar); // baz bar.toUpperCase(); console.log(bar); // baz // Using an array method mutates the array var foo = []; console.log(foo); // [] foo.push("plugh"); console.log(foo); // ["plugh"] // Assignment gives the primitive a new (n
Primitive Wrapper Object in JavaScript :
Except for null
and undefined
, all primitive values have object equivalents that wrap around the primitive values:
String
for the string primitive.Number
for the number primitive.BigInt
for the bigint primitive.Boolean
for the boolean primitive.Symbol
for the symbol primitive.
The wrapper’s valueOf()
method returns the primitive value.
Example of a Primitive String :
Example of a Primitive String wrapped in an Object :