Skip to content

Instantly share code, notes, and snippets.

@cultmind
Last active October 20, 2021 12:15
Show Gist options
  • Save cultmind/24cf84654b49ddb2f7a4fa2da4a77c65 to your computer and use it in GitHub Desktop.
Save cultmind/24cf84654b49ddb2f7a4fa2da4a77c65 to your computer and use it in GitHub Desktop.
object : Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life.
The concept of objects in JavaScript can be understood with real life, tangible objects.
In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup,
Creating a JavaScript Object
Example 1 :
const person = {firstName:"John", lastName:"Doe", age:50 };
person.firstName + " is " + person.age + " years old.";
Result :
'John is 50 years old.'
Spaces and line breaks are not important. An object definition can span multiple lines:
Example 2 :
const person = {
firstName: "John",
lastName: "Doe",
age: 50, };
person.firstName + " is " + person.age + " years old.";
Result :
'John is 50 years old.'
JavaScript for...in Loop
The JavaScript for...in statement loops through the properties of an object.
Syntax
for (let variable in object) {
// code to be executed
}
The block of code inside of the for...in loop will be executed once for each property.
Looping through the properties of an object:
Example :
const person = {
firstname: "John",
lastname: "Doe",
age: 50, } ;
person.nationality = "English";
person.firstname + " is " + person.nationality + ".";
Result :
'John is English.'
delete : The delete keyword deletes a property from an object:
Example :
const person = {
firstName: "John",
lastName: "Doe",
age: 50 };
delete person.age;
person.firstname + " is " + person.age + " years old."
Result :
'John is undefined years old.'
Adding a Method to an Object
Adding a new method to an object is easy .
Example
const person = {
firstName: "Kittu",
lastName: "Sai",
id: 9955,
person.name = function () {
return this.firstName + " " + this.lastName; };
"My father is " + person.name();
Result :
'My father is Kittu Sai.'
Using Built-In Methods
This example uses the toUpperCase() method of the String object, to convert a text to uppercase .
let message = "Hello world!";
let x = message.toUpperCase();
The value of x, after execution of the code above will be:
'HELLO WORLD!'
prototype : The JavaScript prototype property allows you to add new properties to object constructors:
Example
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor; }
Person.prototype.nationality = "English";
Result :
'The nationality of my father is English.'
JavaScript Maps :
1) A Map holds key-value pairs where the keys can be any datatype.
2)A Map remembers the original insertion order of the keys.
3)A Map has a property that represents the size of the map.
We can create a JavaScript Map by:
Passing an Array to new Map()
Create a Map and use Map.set()
new Map() : You can create a Map by passing an Array to the new Map() constructor:
Example
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200] ]);
fruits.get("oranges");
Result :
200
set() : The set() method can also be used to change existing Map values:
Example
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200] ]);
fruits.set("apples", 600);
fruits.get("apples");
Result :
600
has() : The has() method returns true if a key exists in a Map.
Example :
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200] ]);
fruits.has("apples");
Result :
true
typeof() : The typeof operator returns a string indicating the type of the unevaluated operand.
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200] ]);
typeof fruits;
Result :
"object"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment