JavaScript Tutorial

Objects and Arrays In JavaScript | JavaScript Tutorial #42 min read

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 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.

Tanmay Sinha

Author

  • Tanmay Sinha

    Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.

One comment

Leave a Reply