Test your knowledge of JavaScript object literals, properties, methods, prototypes, inheritance, and common array methods. This quiz helps reinforce the foundations for working with complex data in JavaScript.
Object Literal Syntax
What is the correct way to define an object named 'car' with a property 'color' set to 'red' using object literal syntax?
- var car = [ color = 'red' ];
- object car = ('color' : 'red');
- let car = color: 'red';
- const car = { color: 'red' };
- const car = ( color: 'red' );
Accessing Properties
Given const user = { name: 'Sam', age: 20 }, how do you access the 'name' property?
- user('name')
- user[name]
- user:name
- user-u003Ename
- user.name
Adding a New Property
How do you add a new property called 'isAdmin' set to true to an existing object 'employee'?
- employee('isAdmin') = true;
- employee.isAdmin = true;
- employee-u003EisAdmin true;
- employee[isAdmin] = true;
- employee.add('isAdmin', true);
Method Definition in Objects
Which is the correct way to define a method 'greet' inside an object 'person' that logs 'Hello'?
- greet: function() { console.log('Hello'); }
- greet: log('Hello');
- greet[] = { console.log('Hello'); }
- greet = function() { console.log('Hello'); }
- greet() =u003E { console.log('Hello'); }
Using 'this' Keyword
In object methods, what does the 'this' keyword refer to?
- The global object always
- The parent class
- A random variable in the scope
- The most recent function called
- The object the method belongs to
Prototypal Inheritance
What is the main purpose of prototypes in JavaScript?
- To define variable scopes
- To declare constants
- To attach event listeners
- To initialize array values
- To enable inheritance between objects
Array Mapping
What is the output of [2, 4, 6].map(n =u003E n * 2)?
- [2, 8, 36]
- [2, 6, 12]
- [2, 4, 6, 2, 4, 6]
- [4, 6, 8]
- [4, 8, 12]
Filtering Arrays
Which array method returns a new array containing elements that pass a test function?
- join()
- reduce()
- forEach()
- filter()
- splice()
Reducing Arrays
What does the reduce() method do when called on an array like [1, 2, 3]?
- Combines array elements into a single value
- Turns the array into a string
- Increases the length of the array
- Filters out duplicate values
- Sorts the array by default
Using forEach
What is the primary purpose of the forEach() method in JavaScript arrays?
- To filter elements based on a condition
- To execute a provided function once for each array element
- To find the first matching element in the array
- To add new elements to the array
- To combine array elements into a string
Finding Elements
Which method would you use to get the first element greater than 10 in an array?
- sort()
- pop()
- filter()
- find()
- map()
Array Destructuring
How do you assign the first and second values of the array [5, 10] to variables a and b?
- a, b = 5, 10;
- var a = [5], b = [10];
- const (a, b) = [5, 10];
- const [a, b] = [5, 10];
- let a, b = [5, 10];
Object Destructuring
Given const point = { x: 2, y: 3 }, how do you assign 2 to variable x and 3 to y?
- let [x, y] = point;
- const { x, y } = point;
- const x, y = point;
- object {x, y} = point;
- point = (x, y);
Checking Own Properties
What method can be used to check if an object has its own property called 'model'?
- ownsProperty('model')
- getProperty('model')
- propertyExists('model')
- checkProperty('model')
- hasOwnProperty('model')
Prototype Chain Lookup
If a property is not found directly on an object, where does JavaScript look for it next?
- It throws an error instantly
- On the object's prototype
- In the object's constructor
- In the global namespace
- In the array of methods
Object Property Shorthand
Given variables a = 2 and b = 3, how can you define an object using property shorthand?
- const obj = (a, b);
- const obj = [a, b];
- let obj = { :a, :b };
- const obj = { a: a + b: b };
- const obj = { a, b };