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

What is the main purpose of an object in JavaScript?

  • To handle user input from forms
  • To store multiple pieces of related data in a structured format (correct)
  • To create mathematical functions
  • To define global variables for use throughout a script

What are the two key components of an object?

  • Attributes and behaviors (correct)
  • Variables and functions
  • Arrays and strings
  • Loops and conditionals

Which of the following describes an attribute (property) of an object?

  • A loop that iterates over the object's values
  • A function that modifies the object
  • A characteristic or piece of data about the object (correct)
  • An action that the object can perform

What does a 'behavior' (method) define for an object?

<p>An action the object can perform (B)</p> Signup and view all the answers

Which of the following is an example of an object's attribute?

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

Which of the following is a way you can access object properties?

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

Which of the following represents the correct way to call a method on an object?

<p><code>objectName.methodName()</code> (C)</p> Signup and view all the answers

In JavaScript, can you add new properties to an existing object?

<p>Yes, objects are dynamic and can be extended (C)</p> Signup and view all the answers

What does it mean for objects in JavaScript to be 'dynamic'?

<p>They can be easily extended, modified, and have properties removed. (B)</p> Signup and view all the answers

What happens if you try to access a property of an object that does not exist?

<p>The value <code>undefined</code> is returned (B)</p> Signup and view all the answers

Which term refers to built-in objects in JavaScript?

<p>Predefined Objects (C)</p> Signup and view all the answers

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

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

Which of the following is the easiest way to create a JavaScript Array?

<p>Using an array literal (B)</p> Signup and view all the answers

What is the index of the first element in a JavaScript array?

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

What happens when new Array(10) is used in JavaScript?

<p>It creates an empty array with a length of 10 (C)</p> Signup and view all the answers

What is a loop?

<p>A programming construct that repeats a set of instructions (A)</p> Signup and view all the answers

What is the purpose of loops when working with arrays?

<p>To efficiently fill or process array elements (A)</p> Signup and view all the answers

Which JavaScript Date object method returns the year as a four-digit number?

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

What type of information does the JavaScript Date object represent?

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

By default, which time does the JavaScript Date object when created?

<p>System's local time (A)</p> Signup and view all the answers

An object in JavaScript is used to store multiple pieces of related data.

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

Attributes of an object define actions the object can perform.

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

Methods of an object describe the characteristics of an object.

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

A car's brand is an example of an object attribute.

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

The stop() function is an example of a car object's attribute.

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

A person's name can be an attribute in JavaScript.

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

In JavaScript, an object can only contain string values.

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

A variable in JavaScript can hold multiple values.

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

Dot notation is used to access object properties.

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

Bracket notation cannot be used to access object properties.

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

Objects in JavaScript are static and cannot be modified after creation.

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

You can add new properties to an existing JavaScript object.

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

You cannot delete properties from a JavaScript object once they are defined.

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

Math is built-in object that requires the new keyword to use.

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

The Date object does not require the new keyword.

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

JavaScript automatically recognizes String, Number, and Boolean types.

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

All built-in objects inherit from Object.prototype.

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

An array stores multiple values in a single variable.

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

Array elements are indexed starting from 1.

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

The getFullYear() method returns the month as index number.

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

Flashcards

What is an object?

A way to store multiple pieces of related data in a structured format; a container for grouping information.

Attributes (Properties)

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

Behaviors (Methods)

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

Object vs. Simple Variable

Variables hold a single value, objects group related data together in a structured unit.

Signup and view all the flashcards

Dot Notation

Reference object property values using objectName.propertyName.

Signup and view all the flashcards

Bracket Notation

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

Signup and view all the flashcards

Calling Object Methods

Call object methods using objectName.methodName().

Signup and view all the flashcards

Dynamic Objects

In JavaScript, objects are dynamic, allowing addition, modification, and removal of properties.

Signup and view all the flashcards

Updating Object Properties

Update an object's properties by assigning a new value: car.year = 2025;

Signup and view all the flashcards

Adding Properties Dynamically

Add a new property to an object: car.color = "Red";

Signup and view all the flashcards

Removing Properties

Remove a property using: delete car.color;

Signup and view all the flashcards

String, Number, Boolean

An object only if created using the new keyword.

Signup and view all the flashcards

Date Object

Represents date, time, and timezone information. Created using new Date();.

Signup and view all the flashcards

getFullYear()

Can show the year as four digits (yyyy).

Signup and view all the flashcards

getMonth()

Month as index number (0-11).

Signup and view all the flashcards

getDate()

Day as number (1-31).

Signup and view all the flashcards

getHours()

Hour as number (0-23).

Signup and view all the flashcards

getMinutes()

Minute as number (0-59).

Signup and view all the flashcards

getSeconds()

Second as number (0-59).

Signup and view all the flashcards

getMilliseconds()

Millisecond as number (0-999).

Signup and view all the flashcards

Constructor Method

Built-in objects (except Math) have a constructor method.

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

Easiest way to create a JavaScript Array.

Signup and view all the flashcards

Array Indexing

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

Signup and view all the flashcards

What is a Loop?

A programming construct that repeats a set of instructions.

Signup and view all the flashcards

Default Date Object

Captures the system's local time.

Signup and view all the flashcards

Study Notes

Objects in Javascript

  • An object stores related data in a structured format
  • An object is a container that groups different information together

Key Components of an Object

  • Attributes (Properties) describe the characteristics of an object
    • A Car object has attributes like color, brand, model, and speed
  • Behaviors (Methods) define actions an object can perform
    • A Car object can have behaviors like start(), stop(), and accelerate()

Real-Life Object Example: A Car

  • A car has a brand (e.g., "Dodge")
  • A car has a color (e.g., "Red")
  • A car has a speed (e.g., 120 km/h)
  • A car can start the engine (start())
  • A car can accelerate (accelerate())
  • A car can stop (stop())

Real-Life Object Example: A Person

  • A person has a name (e.g., "Ahmed")
  • A person has an age (e.g., 25 years old)
  • A person has a job (e.g., "Teacher")
  • A person can speak or walk

Representing a Person in Javascript

  • Objects are used to represent a person in JavaScript
  • Example:
const person = {
name: "John",
age: 50,
job: "Programmer",
speaking: function() {
return "I am talking";
}
};

Objects vs Simple Variables

  • A variable in JavaScript holds a single value
  • An object groups everything together in one place
  • All the related information is in one structured unit

Accessing Object Properties

  • Object property values can be referenced in two ways:
    • Dot Notation: objectName.propertyName
    • Bracket Notation: objectName['propertyName']

Calling Object Methods

  • Use dot notation to call methods: objectName.methodName()

Extending Objects

  • Objects in JavaScript are dynamic
  • New properties can be added at any time
  • Existing properties can be modified easily
  • Properties can be removed when needed
  • Update an object’s properties by assigning a new value
  • If a property doesn’t exist, it will be added dynamically
  • To delete a property use the keyword delete object.property;

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 as JavaScript recognizes their types automatically
  • All built-in objects inherit from Object.prototype

Create Arrays

  • An Array is a built-in JavaScript object that stores multiple values in a single variable
  • The easiest way to create a JavaScript Array is using an array literal
  • Arrays can store different data types such as numbers, strings, and objects
  • Arrays uses square brackets [] for declaration
  • Array elements are automatically indexed starting from 0 (zero-based index)
  • It is possible to create an array and then populate the array elements
  • The JavaScript keyword new may also be used to create an Array and assign values to it

Unexpected Array Behaviour

  • Using new Array(n) with a single number creates an empty array with n slots, instead of storing the number
  • JavaScript interprets n as the length of the array, not as an element
  • This results in n empty, uninitialized slots

Loop Elements

  • Loops are used to efficiently fill an array, even for large datasets
  • A loop is a programming construct that allows you to repeat a set of instructions multiple times
  • A for loop can populate an array
  • A for loop with a length property can loop through Arrays

Extract Calendar

  • The JavaScript Date object provides methods to extract date components for the year, month name, day of the month, and the day name
  • getFullYear() returns the Year as four digits (yyyy)
  • getMonth() returns the Month as index number (0-11)
  • getDate() returns the Day as number (1-31)
  • getDay() returns the Weekday as index number (0-6)

Get Dates

  • The JavaScript Date object represents date, time, and timezone information
  • Use the keyword new to create a new Date object
  • A new Date captures the system’s local time without ensuring accuracy to UTC or GMT

Extract Time

  • The JavaScript Date object provides methods to extract each of its time components i.e. hour, minute, second, and the millisecond:
  • getHours() returns the Hour as number (0-23)
  • getMinutes() returns the Minute as number (0-59)
  • getSeconds() returns the Second as number (0-59)
  • getMilliseconds() returns the 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 Basics
4 questions
JavaScript Object Properties Quiz
5 questions
DOM Properties and HTML Attributes
15 questions
JavaScript Objects
45 questions
Use Quizgecko on...
Browser
Browser