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 primary purpose of an object in JavaScript?

  • To execute mathematical calculations.
  • To define the styling of HTML elements.
  • To create animations on a webpage.
  • To store multiple pieces of related data in a structured format. (correct)

Which of the following best describes an 'attribute' of an object?

  • A characteristic or property of the object. (correct)
  • A method to destroy the object.
  • An action that the object can perform.
  • A way to hide the object from view.

What is the term used to describe the actions an object can perform?

  • Attributes
  • Methods (correct)
  • Variables
  • Classes

Which notation is used to call a method of an object?

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

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

<p>New properties can be added, modified, or removed at any time. (A)</p> Signup and view all the answers

Which of the following is NOT a way to define an array?

<p>Using curly braces <code>{}</code> (B)</p> Signup and view all the answers

What number does array indexing start from in JavaScript?

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

With the Date object, which method returns the month as an index number?

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

Which method is used to get the year as a four digit number from a Date object?

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

How do you remove new properties from a JavaScript object?

<p>Using <code>delete</code> keyword (A)</p> Signup and view all the answers

An object is designed to store multiple pieces of unrelated data.

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

Attributes describe the characteristics of an object.

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

Behaviors define the actions an object can perform.

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

In JavaScript, a variable can hold multiple values simultaneously without using objects or arrays.

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

Dot notation cannot be used to access object properties in JavaScript.

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

Objects in JavaScript are immutable and cannot be extended or updated.

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

JavaScript only provides user-defined objects.

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

The Math object is an exception, as it does not have a constructor method.

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

Arrays cannot store different data types.

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

The getDate() method retrieves the weekday as an index number.

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

Which data structure is most suitable for grouping multiple pieces of related data in JavaScript?

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

If myObject has a property named resource what is the correct way to access this property using bracket notation?

<p>myObject[&quot;resource&quot;] (C)</p> Signup and view all the answers

Given const car = { brand: "Toyota", start: function() { return "Engine started"; } };, how is the start behavior of the car object correctly called?

<p>car.start() (B)</p> Signup and view all the answers

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

<p>Properties can be added, modified, and removed after creation. (C)</p> Signup and view all the answers

How can a new property named color with a value of "blue" be added to an existing object named myObject?

<p>myObject.color = &quot;blue&quot;; (A)</p> Signup and view all the answers

What is the correct way to remove the property size from the object myObject?

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

Which of the following is true about JavaScript built-in objects?

<p>Most built-in objects (except <code>Math</code>) have a constructor method. (B)</p> Signup and view all the answers

Consider const myArray = new Array(5);. What will be the content of myArray?

<p><code>[ &lt;5 empty slots&gt; ]</code> (A)</p> Signup and view all the answers

Given const dates = new Date();, which method returns the day of the month as a number (1-31)?

<p>dates.getDate() (A)</p> Signup and view all the answers

Which action does the JavaScript Date object perform by default?

<p>Captures the system's local time without ensuring accuracy to UTC or GMT. (D)</p> Signup and view all the answers

JavaScript objects are designed to store multiple pieces of related data.

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

In JavaScript, object properties cannot be modified once the object has been created.

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

Dot notation cannot be used to call methods on JavaScript objects.

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

In JavaScript, bracket notation objectName['propertyName'] is a valid method for accessing object properties.

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

JavaScript arrays can only store elements of the same data type.

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

Built-in JavaScript objects like Math do not have a constructor method.

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

The getFullYear() method returns the month as an index number (0-11).

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

Using the new keyword with String, Number, or Boolean is the recommended approach in JavaScript.

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

A JavaScript Date object, by default, captures the system's time relative to UTC or GMT standards.

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

All built-in JavaScript objects inherit from Object.prototype.

<p>True (A)</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.

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

Why use objects over simple variables?

An object groups everything together in one place, acting as a structured unit.

Signup and view all the flashcards

How do you access object properties?

Reference object property values using dot notation or bracket notation.

Signup and view all the flashcards

How do you call object methods?

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

Signup and view all the flashcards

Can Objects Be Extended & Updated Easily?

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

Signup and view all the flashcards

Using 'new' with Built-in Objects

Built-in objects have a constructor method, but avoid 'new' with String, Number, or Boolean.

Signup and view all the flashcards

What is a Date object?

The JavaScript Date object represents date, time, and timezone information.

Signup and view all the flashcards

What is an JavaScript Array object?

Each Array object has a length property and methods that can be used to manipulate the elements in the array.

Signup and view all the flashcards

JavaScript Prototype Inheritance

Built-in objects inherit from Object.prototype.

Signup and view all the flashcards

getFullYear()

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

getDay()

Weekday as index number (0-6).

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

What is an object method?

A function that belongs to an object.

Signup and view all the flashcards

What is a JavaScript Array?

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

Signup and view all the flashcards

new Array(n) behavior

Creates an empty array with n uninitialized slots.

Signup and view all the flashcards

getMilliseconds()

Millisecond as number (0-999).

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 instructions multiple times.

Signup and view all the flashcards

Study Notes

  • Objects in JavaScript consist of multiple pieces of related data in a structured format, acting as a container.

Key Components of an Object

  • Attributes (Properties): Describe the characteristics of an object.
    • A "Car" object can have attributes like color, brand, model, and speed.
  • Behaviors (Methods): Define actions that an object can perform.
    • A "Car" object can have behaviors like start(), stop(), and accelerate().

Real-Life Examples of Objects

  • Car:
    • A car has a brand such as "Dodge".
    • A car has a color, such as "Red".
    • A car has a speed, such as 120 km/h.
    • A car can start the engine, accelerate, and stop.
  • Person:
    • A person has a name such as "Ahmed".
    • A person has an age, such as 25 years old.
    • A person has a job, such as "Teacher".
    • A person can perform actions like speaking or walking.

Representing a Person Object in JavaScript

  • Created using const keyword:
    • The object "person" has properties like name ("John"), age (50), and job ("Programmer").
    • It also has a method called "speaking" that returns "I am talking".

Objects vs Simple Variables

  • Variables in JavaScript hold a single value.
    • Example of variables: name = "John", age = 25, job = "Programmer"
  • Managing data separately becomes hard with many people, so objects are used instead.
  • Objects group everything together 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

  • Dot notation is used to call object methods: objectName.methodName()

Sample Car Object and Property Access

  • A "car" object is defined with properties like brand, model, year, and a start function.
    • The brand can be accessed through console.log(car.brand), outputting "Dodge".
    • The model can be accessed through console.log(car["model"]), outputting "Challenger".
    • The start function can be called using car.start(), outputting "Car started!".

Extending Objects in JavaScript

  • Objects are dynamic and can be extended and updated easily.
    • New properties can be added at any time.
    • Existing properties can be easily modified.
    • Properties can be removed when needed.
  • An object's properties can be updated by assigning a new value.
    • For example, car.year = 2025; would update the year property of the car object.
  • If a property does not exist, adding it will dynamically add it:
    • For example, car.color = "Red"; would add a color property to the car object
  • The "delete" keyword is used to remove properties.
    • For example, delete car.color; would remove the color property. console.log(car.color) would then output "undefined".

Built-in JavaScript Objects

  • String: An object only if created using the new keyword.
  • Number: An object only if created using the new keyword.
  • Boolean: An object only if created using the new keyword.
  • Object: Defined by the user.
  • Date: Contains date and time components.
  • Array: Stores indexed items of data.
  • RegExp: Describes a regular expression pattern.
  • Math: Provides math properties and methods.
  • Error: Supplies details of an error.
  • 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.

JavaScript Prototype Inheritance

  • 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.
  • The easiest way to create an Javascript Array is using array literal
  • Different data types (numbers, strings, objects, etc.) can be stored.
  • Square brackets [] are used for declaration.
  • Elements are automatically indexed starting from 0 (zero-based index).
    • For example: let colors = ["Red", "Blue", "Yellow"];
  • An array can be also created and the elements can be specified afterwards:
    • For example: const cars = []; cars[0]= "Saab"; cars[1]= "Volvo"; cars[2]= "BMW";
  • An array can be also created using the Javascript keyword "new":
    • 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.
    • JavaScript interprets the single number passed to the Array constructor as the length of the array, resulting in empty, uninitialized slots.

Loops

  • Loops efficiently fill an array, even for large datasets.
  • Use a for loop to efficiently populate an array.
    • Example:
const numbers = [];
for (let i = 0; i < 10; i++) {
numbers[i] = i * 2;
}
console.log(numbers);
  • A for loop with length property can be also used:
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
  • Date objects have methods for getting the date and time components.
  • 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.

Methods extracting date

  • getFullYear(): Year as four digits (yyyy)
  • getMonth(): Month as index number (0-11)
  • getDate(): Day as number (1-31)
  • getDay(): Weekday as index number (0-6)

Methods extracting time

  • getHours(): Hour as number (0-23)
  • getMinutes(): Minute as number (0-59)
  • getSeconds(): Second as number (0-59)
  • getMilliseconds(): 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