Objects help group related information together in one place. They let you store and organize data with names.
Object creation in Javascript
Introduction
Syntax
Javascript
const objectName = { key1: value1, key2: value2 // more key-value pairs };
Keys are names for the data and values can be any type like numbers, strings, or even other objects.
Use commas to separate each key-value pair inside the curly braces.
Examples
person with two properties: name and age.Javascript
const person = { name: "Alice", age: 30 };
car with three properties: brand, year, and isElectric.Javascript
const car = { brand: "Toyota", year: 2020, isElectric: false };
Javascript
const emptyObject = {};Sample Program
This program creates a book object with three properties. Then it prints each property to the console.
Javascript
const book = { title: "JavaScript Basics", author: "Sam", pages: 150 }; console.log("Book title:", book.title); console.log("Author:", book.author); console.log("Pages:", book.pages);
Important Notes
You can access object properties using dot notation like objectName.key.
Objects can hold other objects or arrays as values for more complex data.
Summary
Objects group related data with named keys.
Create objects using curly braces and key-value pairs.
Access data inside objects with dot notation.
Practice
1. Which of the following is the correct way to create an object with a key
name and value "Alice"?easy
Solution
Step 1: Understand object literal syntax
Objects in JavaScript are created using curly braces with key-value pairs inside, like{ key: value }.Step 2: Identify the correct syntax for key-value pair
The keynameand value"Alice"should be inside curly braces separated by a colon.Final Answer:
const obj = { name: "Alice" }; -> Option AQuick Check:
Object literal = curly braces with key: value [OK]
Hint: Objects use curly braces with key: value pairs [OK]
Common Mistakes:
- Using parentheses instead of curly braces
- Using array brackets for objects
- Confusing arrow functions with object creation
2. Which of the following is the correct syntax to create an empty object in JavaScript?
easy
Solution
Step 1: Recall empty object syntax
An empty object is created using empty curly braces{}.Step 2: Check each option
const obj = {}; uses{}, which is the correct syntax. Others use parentheses, array brackets, or invalid syntax.Final Answer:
const obj = {}; -> Option AQuick Check:
Empty object = {} [OK]
Hint: Empty objects use just curly braces {} [OK]
Common Mistakes:
- Using parentheses instead of curly braces
- Using array brackets for objects
- Trying to use new Object[] which is invalid
3. What will be the output of the following code?
const person = { name: "Bob", age: 30 };
console.log(person.name);medium
Solution
Step 1: Understand object property access
Using dot notationperson.nameaccesses the value of the keynamein the object.Step 2: Identify the value of
The value ofnamenameis the string "Bob".Final Answer:
"Bob" -> Option CQuick Check:
person.name = "Bob" [OK]
Hint: Dot notation gets value by key name [OK]
Common Mistakes:
- Confusing key name with value
- Using brackets without quotes incorrectly
- Expecting age instead of name
4. Find the error in the following code that tries to create an object:
const car = { brand = "Toyota", year: 2020 };medium
Solution
Step 1: Check object key-value syntax
In objects, keys and values are separated by a colon:, not an equals sign=.Step 2: Identify the incorrect part
The code usesbrand = "Toyota"which is invalid syntax for object properties.Final Answer:
Using = instead of : for the key brand -> Option BQuick Check:
Object key-value separator = colon : [OK]
Hint: Use colon : between key and value in objects [OK]
Common Mistakes:
- Using = instead of : in object literals
- Thinking semicolon is mandatory inside object
- Confusing variable declaration keywords
5. You want to create an object
student with keys name, age, and grades (an array). Which code correctly creates this object?hard
Solution
Step 1: Verify object literal syntax with multiple keys
Objects use curly braces with keys and values separated by colons, and multiple pairs separated by commas.Step 2: Check each option for correct syntax and data types
const student = { name: "Eva", age: 22, grades: [90, 85, 88] }; correctly uses colons and commas, andgradesis an array. const student = { "name" = "Eva", "age" = 22, "grades" = [90, 85, 88] }; uses equals signs which is wrong. const student = [ name: "Eva", age: 22, grades: [90, 85, 88] ]; uses array brackets instead of curly braces. const student = { name: "Eva", age: "22", grades: "90, 85, 88" }; uses a string for grades instead of an array.Final Answer:
const student = { name: "Eva", age: 22, grades: [90, 85, 88] }; -> Option DQuick Check:
Object with array value uses colon and brackets [OK]
Hint: Use colons for keys and arrays inside objects with brackets [OK]
Common Mistakes:
- Using = instead of : for keys
- Using arrays brackets instead of curly braces for objects
- Putting arrays as strings instead of actual arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
