JavaScript Objects

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In JavaScript, what is the primary purpose of an object?

  • To execute complex mathematical calculations and algorithms.
  • To store multiple pieces of related data in a structured format. (correct)
  • To manage user interface elements and handle events.
  • To define global variables for use across multiple scripts.

Which of the following best describes the two fundamental components of a JavaScript object?

  • Classes and instances
  • Data types and operators
  • Attributes (properties) and behaviors (methods) (correct)
  • Variables and functions

Consider a Book object. Which of the following options lists appropriate attributes (properties) for this object?

  • `checkAvailability()`, `returnBook()`, `borrowBook()`
  • `title()`, `author()`, `pages()`
  • `readBook()`, `getBookTitle()`, `getBookAuthor()`
  • `title`, `author`, `numberOfPages` (correct)

Considering the same Book object, which of the following options lists appropriate behaviors (methods) for this object?

<p><code>isAvailable()</code>, <code>borrowBook()</code>, <code>returnBook()</code> (B)</p> Signup and view all the answers

In JavaScript, how does using an object to store data differ from using individual variables?

<p>Objects group related data into a structured unit, while variables hold single, isolated values. (B)</p> Signup and view all the answers

Given a JavaScript object named employee, which contains properties like name and age, how would you access the employee's name using dot notation?

<p>employee.name (A)</p> Signup and view all the answers

Using the same employee object, how would you access the employee's age using bracket notation?

<p>employee['age'] (B)</p> Signup and view all the answers

If you have a JavaScript object car with a method called startEngine, how do you call this method?

<p>car.startEngine() (A)</p> Signup and view all the answers

What does it mean that JavaScript objects are 'dynamic'?

<p>You can add, modify, and remove properties from them at any time. (C)</p> Signup and view all the answers

Given an existing object myObject, how can you add a new property address with a value of '123 Main St'?

<p>myObject.address = '123 Main St'; (B)</p> Signup and view all the answers

If you want to change the value of a property named color in an object called circle to 'blue', how would you do it?

<p>circle.color = 'blue'; (A)</p> Signup and view all the answers

How can you remove a property named size from an object named square?

<p>delete square.size; (C)</p> Signup and view all the answers

Which of the following is NOT a built-in JavaScript object?

<p><code>Document</code> (B)</p> Signup and view all the answers

Which built-in JavaScript objects should typically be created using the new keyword?

<p><code>Date</code> and <code>Error</code> (D)</p> Signup and view all the answers

What is the significance of Object.prototype in JavaScript?

<p>All built-in objects inherit from it, providing them with common functionalities. (C)</p> Signup and view all the answers

What is the primary purpose of an array in JavaScript?

<p>To store multiple values in a single variable, accessed by index. (A)</p> Signup and view all the answers

Given let myArray = [10, 20, 30];, how would you access the second element (value 20) of the array?

<p>myArray[1] (C)</p> Signup and view all the answers

If you create an array using const numbers = new Array(5);, what will be the content of the array?

<p>An array with 5 empty slots (uninitialized). (B)</p> Signup and view all the answers

What is the main benefit of using loops when working with arrays?

<p>Loops allow you to execute a set of instructions repeatedly, making it efficient to process each element in the array. (B)</p> Signup and view all the answers

Given the array const values = [5, 10, 15];, which loop would correctly log each value to the console along with its index?

<p><code>for (let i = 0; i &lt; values.length; i++) { console.log('Index', i, values[i]); }</code> (C)</p> Signup and view all the answers

What does the JavaScript Date object represent?

<p>Date, time, and timezone information. (C)</p> Signup and view all the answers

How is a new Date object typically created in JavaScript?

<p><code>const event = new Date();</code> (B)</p> Signup and view all the answers

Which method of the Date object returns the year as a four-digit number (yyyy)?

<p><code>getFullYear()</code> (D)</p> Signup and view all the answers

Which Date object method retrieves the month as an index number?

<p><code>getMonth()</code> (A)</p> Signup and view all the answers

Which Date object method is used to extract the hour?

<p><code>getHours()</code> (C)</p> Signup and view all the answers

In JavaScript, an object is used to store multiple pieces of related data in a structured format.

<p>True (A)</p> Signup and view all the answers

In JavaScript, the attributes of an object define the actions that an object can perform.

<p>False (B)</p> Signup and view all the answers

Behaviors of objects, also know as methods, define the characteristics of an object.

<p>False (B)</p> Signup and view all the answers

In JavaScript, a car object can have attributes like color, brand, and model.

<p>True (A)</p> Signup and view all the answers

In JavaScript, actions that a car can perform, such as start, stop, and accelerate are known as attributes.

<p>False (B)</p> Signup and view all the answers

A JavaScript variable can simultaneously hold multiple values of different data types without being part of an object.

<p>False (B)</p> Signup and view all the answers

Using bracket notation, this is a valid way to access an object's property: objectName('propertyName')

<p>False (B)</p> Signup and view all the answers

You can reference object property values in JavaScript using dot notation or bracket notation.

<p>True (A)</p> Signup and view all the answers

In JavaScript, you can only use bracket notation and not dot notation to call methods.

<p>False (B)</p> Signup and view all the answers

JavaScript objects are static, meaning you cannot add new properties after an object is created.

<p>False (B)</p> Signup and view all the answers

When updating an existing property of an object in JavaScript, you must use a special method to ensure the change is applied correctly.

<p>False (B)</p> Signup and view all the answers

In Javascript, built-in objects are not dynamic, meaning you cannot add new properties to them at any time.

<p>True (A)</p> Signup and view all the answers

In JavaScript, properties can be removed from objects using the delete keyword.

<p>True (A)</p> Signup and view all the answers

In JavaScript, String, Number, and Boolean are primitive data types and therefore cannot be instantiated as objects using the new keyword.

<p>False (B)</p> Signup and view all the answers

In JavaScript, all built-in objects inherit from Object.prototype.

<p>True (A)</p> Signup and view all the answers

In JavaScript, an Array can only store values of the same data type.

<p>False (B)</p> Signup and view all the answers

In JavaScript, elements in an array are automatically indexed starting from 1.

<p>False (B)</p> Signup and view all the answers

JavaScript interprets a single numeric argument passed to the Array() constructor as the value to be stored as the first element in the array.

<p>False (B)</p> Signup and view all the answers

A JavaScript Date object, by default, captures the system's local time without ensuring accuracy to UTC or GMT.

<p>True (A)</p> Signup and view all the answers

The JavaScript getDay() method returns the day of the month as a number between 1 and 31.

<p>False (B)</p> Signup and view all the answers

Flashcards

What is an object?

A way to store multiple pieces of related data, grouping different information together.

What are Attributes (Properties)?

Describe the characteristics of an object. (e.g. color, brand, model, speed)

What are Behaviors (Methods)?

Define actions that an object can perform (e.g. start(), stop(), accelerate()).

Single value

In JavaScript a variable holds a _____ value.

Signup and view all the flashcards

How do Objects group data?

An object groups everything together in one place.

Signup and view all the flashcards

Accessing Object Properties

You can reference object property values in two ways: Dot and Bracket.

Signup and view all the flashcards

Calling Object Methods

Use _____ notation to call methods: objectName.methodName()

Signup and view all the flashcards

JavaScript Objects Are Dyamic

Objects in JavaScript are _____, meaning you can add, modify, and remove properties.

Signup and view all the flashcards

Most built-in objects

These objects have a constructor method.

Signup and view all the flashcards

The keyword 'new'

Only use new with ____ and Error.

Signup and view all the flashcards

Avoid?

Avoid using new with String, Number, or Boolean since JavaScript recognizes their types automatically.

Signup and view all the flashcards

Object.prototype

All built-in objects inherit from this.

Signup and view all the flashcards

What is an Array?

A built-in JavaScript object that stores multiple values in a single variable.

Signup and view all the flashcards

Array literal

Using an _____ _____ is the easiest way to create a JavaScript Array.

Signup and view all the flashcards

Zero

Elements are automatically indexed starting from _____ (zero-based index).

Signup and view all the flashcards

Empty Array

Using new Array(n) with a single number creates what?

Signup and view all the flashcards

What is a loop?

A programming construct that allows you to repeat a set of instructions multiple times.

Signup and view all the flashcards

JavaScript Date Object

A JavaScript Date object provides separate methods to extract each of its date components.

Signup and view all the flashcards

getFullYear()

Returns the year as four digits (yyyy).

Signup and view all the flashcards

getMonth()

Returns the month as index number (0-11).

Signup and view all the flashcards

getDate()

Returns the day as number (1-31).

Signup and view all the flashcards

JavaScript Date Object for Time

A JavaScript Date object provides separate methods to extract each of its time components.

Signup and view all the flashcards

getHours()

Returns the hour as number (0-23).

Signup and view all the flashcards

getMinutes()

Returns the minute as number (0-59).

Signup and view all the flashcards

getSeconds()

Returns the second as number (0-59).

Signup and view all the flashcards

Dot Notation

Reference object property values via objectName.propertyName.

Signup and view all the flashcards

Bracket Notation

Reference object property values via objectName['propertyName'].

Signup and view all the flashcards

Update Object Properties

Assign it a new value.

Signup and view all the flashcards

Add Object Properties

Assign a value to a property that doesn't exist.

Signup and view all the flashcards

Remove Object Properties

delete object.propertyName;

Signup and view all the flashcards

String object

An object only if created using the new keyword.

Signup and view all the flashcards

Number object

An object only if created using the 'new' keyword.

Signup and view all the flashcards

Boolean object

An object only if created using the 'new' keyword.

Signup and view all the flashcards

Object

An object defined by you.

Signup and view all the flashcards

Array

An object storing a collections of data.

Signup and view all the flashcards

RegExp

Describe a regular expression pattern.

Signup and view all the flashcards

Math object

Providing math properties and methods.

Signup and view all the flashcards

Error object

One object that supplies details of an error.

Signup and view all the flashcards

What does Date object express?

A JavaScript Date object represents _____ ,time, and timezone information.

Signup and view all the flashcards

New Date() default

Captures the system's local time.

Signup and view all the flashcards

Study Notes

Objects in JavaScript

  • Objects store multiple pieces of related data in a structured format
  • Objects can be thought of as a container that groups different information together
  • Objects have attributes (properties) that describe its characteristics
    • Example: A Car object has attributes like color, brand, model, and speed
  • Objects also have behaviors (methods) that define actions an object can perform
    • Example: A Car object can have behaviors like start(), stop(), and accelerate()
  • A real-life example of an object is a car, which has a brand (e.g., "Dodge"), a color (e.g., "Red"), a speed (e.g., 120 km/h), and can perform actions like starting the engine, accelerating, or stopping
  • Another real-life example of an object is a person which has a name (e.g., "Ahmed"), an age (e.g., 25 years old), a job (e.g., "Teacher"), and actions they can perform, like speaking or walking
  • JavaScript variables holds a single value
  • Objects groups everything together in one place.
  • Custom objects are assigned properties as a comma-separated list of name:value pairs within {} curly brackets.

Accessing and extending Objects

  • You can reference object property values in two ways: dot or bracket notation
    • Dot Notation: objectName.propertyName
    • Bracket Notation: objectName['propertyName']
  • Use dot notation to call methods: objectName.methodName()
  • JavaScript objects are dynamic, meaning you can extend or update them easily
  • You can add new properties at any time
  • You can update an object’s properties simply by assigning a new value
  • If a property doesn’t exist, it will be added dynamically
  • You can remove properties when needed using the delete keyword.
  • Object property values can be referenced using dot notation syntax or by quoting their name between [] square brackets.
  • Object methods are called by appending () parentheses after the object’s method name.

Built In Objects

  • Most built-in objects (except Math) have a constructor method
  • Use new only with Date and Error
  • Avoid using new with String, Number, or Boolean since JavaScript recognizes their types automatically
  • All built-in objects inherit from Object.prototype

Arrays

  • An array is a built-in JavaScript object that stores multiple values in a single variable
  • Using an array literal is the easiest way to create a JavaScript Array
  • Arrays can store different data types (numbers, strings, objects, etc.)
  • In JavaScript Arrays, square brackets [] are used for declaration
  • Elements are automatically indexed starting from 0 (zero-based index)
    • For example: let colors = ["Red", "Blue", "Yellow"];
  • Arrays can also be created by first declaring and then providing elements.
    • For example:
const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";
  • You can also use the JavaScript Keyword new to create and assign values to Arrays
    • For example: const cars = new Array("Saab", "Volvo", "BMW");
  • Using new Array(n) with a single number creates an empty array with n slots, instead of storing the number
    • For example: const cars = new Array(10); creates 10 empty, uninitialized slots.
  • JavaScript interprets a single number as the length of the array, not as an element
  • The JavaScript built-in Array object stores items in individual elements that are numbered starting at zero.
  • Values can be assigned to an array as a comma-separated list within [] square brackets.
  • Each Array object has a length property and methods that can be used to manipulate the elements in the array.

Loops

  • A loop is a programming construct that allows you to repeat a set of instructions multiple times
  • Loops allow you to efficiently fill an array, even for large datasets
    • An example using a for loop to populate an array:
const numbers = [];
for (let i = 0; i < 10; i++) {
  numbers[i] = i * 2;
}
console.log(numbers);
  • Looping Through Arrays:
const numbers = [10, 20, 30, 40,50];
for (let i = 0; i < numbers.length; i++) {
  console.log('Index', i, numbers[i]);
}

JavaScript Date object

  • The JavaScript Date object represents date, time, and timezone information
  • A new Date object is created using the new keyword: const currentDate = new Date();
  • By default, it captures the system’s local time without ensuring accuracy to UTC or GMT
  • The JavaScript Date object provides separate methods to extract each of its date components for the year, month name, day of the month, and the day name.
  • The JavaScript built-in Date object provides separate methods to extract each of its date and time components.

Methods return values

  • getFullYear() returns Year as four digits (yyyy)
  • getMonth() returns Month as index number (0-11)
  • getDate() returns Day as number (1-31)
  • getDay() returns Weekday as index number (0-6)
  • getHours() returns Hour as number (0-23)
  • getMinutes() returns Minute as number (0-59)
  • getSeconds() returns Second as number (0-59)
  • getMilliseconds() returns Millisecond as number (0-999)

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

JavaScript Object Properties Quiz
5 questions
JavaScript Built-in Objects Quiz
24 questions

JavaScript Built-in Objects Quiz

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
DOM Properties and HTML Attributes
15 questions
JavaScript Objects
40 questions
Use Quizgecko on...
Browser
Browser