Objects are an important part of JavaScript as just about everything you do in the language involves some type of object. In this article I’m going to discuss why object-based computing is important and how to compute with objects in JavaScript.

Why You Need Objects

Imagine you are teaching an introductory JavaScript course, you have ten students in the course, and you want to keep track of all their grades so you can compute each student’s average and the class average. If all you have to work with are variables, you will need a variable for each student’s tests, meaning that a class of ten students with three tests will require thirty variables.

You can simplify this a little by putting the test scores into a list, but what about the other information you need to keep track of, such as names, id numbers, majors, etc?

The solution is to create a composite data type that can store more than one type of data. This is where JavaScript objects come in. With an object, you can store a student’s name, their id number, a list of their grades, and any other data you want inside one object. This will keep your data organized and make your programs that access this data easier to read, easier to debug, and more efficient.

How to Create JavaScript Objects

Before I show you how to create objects, let me define a few terms. An object is a piece of data that defines a set of properties and their values. Object properties can be of any JavaScript data type and most objects contain properties of multiple data types.

#learning-javascript #programming #learn-to-program #javascript

Learning JavaScript: Computing with Objects
1.15 GEEK