Javascript Objects
Objects are everywhere. An object has different meanings such as a thing, material, person or thing's specified action. But when it comes to the programming world, it could be a variable, data structure, method, or function. It usually refers to state and behavior in any context.
1) It should not be a keyword such as if, while, for, etc.
2) It should start with lowercase and follow with uppercase when the variable has multiple words. Ex: brandName (not BrandName / Brandname).
3) It should not contain space and a hyphen. Ex: brand Name or brand-name.
4) If the value is a string, it should be within double quotes "", numbers and boolean values would be as it is.
5) Keys and values are separated with a semicolon (;).
In Javascript, an object is a collection of keys and values. Keys represent the variable's name and the values are the data stored on the variable. It is considered to be a dictionary. It can have a variable, an object, and even a function in it. We call it an object literal.
Javascript is a prototype-based object-oriented language and because of this, each object will have the prototype set by default when assigned along with the Key: Value pair.
For example, let me explain with a mobile phone. A mobile phone has many characteristics to describe. We can collate all/few and describe it as a mobile phone. Some variables are brand name, size, color, screen resolution, OS, Storage, and many more. In JS, we call everything Keys and the answer to these keys is Values.
//object
let mobile = {
brandName: "Samsung Galaxy S23",
sizeInInches: 6.1,
storageInGB: 128,
operatingSystem: "Android13",
isLaunched: true,
defaultApps: {
whatsApp: "yes",
faceBook: "yes",
count: 10
}
}
There are 3 data types used in JS: 'let', 'var', and 'const'. I used 'let' here.
Few rules when declaring a variable in an object for the best practices:
2) It should start with lowercase and follow with uppercase when the variable has multiple words. Ex: brandName (not BrandName / Brandname).
3) It should not contain space and a hyphen. Ex: brand Name or brand-name.
4) If the value is a string, it should be within double quotes "", numbers and boolean values would be as it is.
5) Keys and values are separated with a semicolon (;).
6) Each key-value pair is separated with a comma (,).
An object can have n number of object declarations. "defaultApps" is another object declared inside the object "mobile". All the variables must be inside the curly braces.
Calling the variables of an object has 2 different options.
1) Dot notation
//dot notation
console.log(mobile.brandName);
console.log(mobile.defaultApps.whatsApp);
2) Bracket notation
//bracket notation
console.log(mobile['sizeInInches']);
console.log(mobile.defaultApps['count']);
When running the entire code, the output will be
[Running] node "c:\Users\sukan\source\repos\Javascript\mobile.js"
Samsung Galaxy S23
yes
6.1
10
The object is simple and interesting to learn when you understand the importance of naming and assigning correctly.
Comments
Post a Comment