Objects are collections of `key: value` pairs. These `key: value` pairs are also called properties.
JavaScript objects enable you to store, manipulate, and send data across a network.
In objects, you can store in-depth, composite/complex data.
⚡ Create objects
In JavaScript, creating an object is quite easy. To create an object, all you need is a set of curly braces `{ }` and within that, you have to write `key: value` pairs separated with a comma `,`.
Here's an example to understand it better:
If you need to access any of the object's properties. The value of a property can be accessed by using its `key`. There are two methods to access the object's properties:
🔸 The dot notation
This is the most commonly used method.
Example:
console.log(blogPost.title); // Output: Objects in JavaScript
🔸 The bracket notation
You can use bracket notation to replace the dot(`.`) with square brackets `[ ]` and then convert the `key` name to a `string`.
Example:
console.log(blogPost["title"]); // Output: Objects in JavaScript
⚡ Nested objects
The object within an object is called a nested object.
Here's an example:
⚡ Objects methods
In JavaScript, creating functions within objects is referred to as methods.
Example:
Here's how you can invoke this function:
player.greet();
⚡ Object destructuring(ES6)
JavaScript has a nice feature called object destructuring that lets you extract properties from objects and bind them to variables. It is capable of extracting many properties in one statement, accessing properties from nested objects, and ...
settinga default value in the absence of a property.
Syntax:
const { property } = object;
Example:
Check out this repository dedicated to frontend development, where I share informative content, tutorials, and practical code examples related to best practices in frontend development.
In JavaScript, arrays are ordered lists. A single variable called an array is used to hold various elements. It is frequently used if we need to store a list of elements and access them using a single variable.
Create an Array:
let arr = [10, 20, 30] ;
⚡ Accessing Array Elements
The values in an array are referred to as elements. Utilizing the index number, you can get to a specific array element.
Arrays are 0 indexed. In other words, you would use the 0 index to access the first item in the list.
⚡ Conditional Statements
Sometimes, depending on the condition, you might want to perform different actions. To do this, you can use conditional statements in JavaScript.
The following are different types of CS:
✧ if
✧ else
✧ else-if
✧ switch
✧ ternary operator
✧ if
The `if` statement is used to write a block of code that will run only if a certain condition is true.
Example:
if (50 > 5) {
let result = "Yes, 50 is greater than 5!";
}
// Output: "Yes, 50 is greater than 5!"
Designing web pages has become easy with grids. It can have items placed on it vertically, horizontally, or both at once. You can arrange items however you want, even stacked.
Grid terminology
It is important to understand Grid terminology before learning about its properties.