In the previous tutorial, we learned about DOM (Document Object Model) In JavaScript) which is a programming interface for HTML That is used to manipulate content using programming languages like JavaScript. DOM Allows Us to add, remove, or modify elements and attributes in your HTML Document. In this post, We Will Be Learning About Objects and Arrays In Javascript.
What You’ll Learn:
What are Objects In JavaScript?
In JavaScript, an item is a set of key-fee pairs that constitute a single entity. You can use items to shop data, outline methods, and prepare your code.
To create an object in JavaScript, you can use `object` literal syntax or object constructors. Here’s an example using object literal syntax:
Creating An Empty Object
// Create an empty object
var obj = {};
Creating an Object With Properties
// Create an object with properties
var obj = {
name: “John”,
age: 30,
greet: function() {
console.log(“Hello, my name is ” + this.name);
}
};
Accessing an Object
To access the properties of an object, you can use dot notation or bracket notation. For example:
// Access the “name” property using dot notation
console.log(obj.name);
// Access the “age” property using bracket notation
console.log(obj[“age”]);
// Call the “greet” method
obj.greet();
What is an Array In JavaScript?
An array is a special type of object that represents a list of values. You can use arrays to store data, perform calculations, and organize your code.
To create an array in JavaScript, you can use the array literal syntax or the `Array` constructor. Here is an example using the array literal syntax:
Creating an Empty Array
// Create an empty array
var arr = [];
Creating an Array with Values
// Create an array with values
var arr = [1, 2, 3, 4, 5];
To access the elements of an array, you can use the index of the element.
- The JS Developer’s Podcast [EP: 1] Introduction to JavaScript - September 27, 2024
- Which is the Best Operating System for Programming? Here are 3 OS to Consider - August 24, 2024
- Top 10 Cybersecurity Threats to Watch Out for in 2024 - August 17, 2024
Pingback: JSON and API calls In JavaScript | JavaScipt Tutorial #5 - Mr Programmer