Master JavaScript through hands-on coding challenges. Write real code, get instant feedback, and build practical web development skills.
Learn the core building blocks of JavaScript programming through hands-on practice.
fruits - Initialize it with at least 3 fruits: "apple", "banana", "orange". Arrays in JavaScript are created using square brackets [] and can hold any type of data. Arrays are zero-indexed, meaning the first element is at index 0.console.log(fruits) to see your starting array. You can also print the length with fruits.length.push() - The push() method adds one or more elements to the END of an array and returns the new length. Example: fruits.push("mango") adds "mango" at the end.pop() - The pop() method removes the LAST element from an array and returns that element. This modifies the original array. You can store the removed item: const removed = fruits.pop().fruits[0] gets the first element, fruits[1] gets the second, etc. Print a specific element with console.log(fruits[0]).indexOf() - The indexOf() method returns the first index at which a given element is found, or -1 if not found. Example: fruits.indexOf("banana") returns 1 if banana is the second element.unshift() adds to the beginning, shift() removes from the beginningincludes() returns true/false if an element exists (simpler than indexOf for checking existence)slice() returns a shallow copy of a portion of an arraysplice() can add/remove elements at any positionslice() (non-destructive) with splice() (destructive)=== (compares references, not contents)Original array: [ 'apple', 'banana', 'orange' ]
Array length: 3
After push: [ 'apple', 'banana', 'orange', 'mango' ]
Popped element: mango
First fruit: apple
Index of banana: 1
const fruits = ["apple", "banana", "orange"];fruits.push("mango");const last = fruits.pop();fruits[0] returns first elementfruits.indexOf("banana") returns positionfruits.length returns number of elementsReview feedback below
greet - Function declarations use the function keyword followed by a name. This function should take a name parameter and return a greeting string like "Hello, [name]!". Function declarations are hoisted, meaning they can be called before they're defined in the code.function greet(name) {
return "Hello, " + name + "!";
}multiply - Function expressions assign an anonymous function to a variable. This function should take two parameters a and b and return their product. Unlike declarations, function expressions are NOT hoisted.const multiply = function(a, b) {
return a * b;
};square - Arrow functions provide a shorter syntax introduced in ES6. This function should take a number n and return its square (n * n). Arrow functions don't have their own this binding.const square = (n) => n * n;
// Or with braces: const square = (n) => { return n * n; };sayHello that takes a name with default value "World".const sayHello = (name = "World") => `Hello, ${name}!`;return and curly bracesn => n * 2`Hello, ${name}!`return statement (function returns undefined by default)this inside arrow functions (they don't bind their own this)Greeting: Hello, Alice!
Product: 12
Square of 5: 25
Default greeting: Hello, World!
Custom greeting: Hello, JavaScript!
function greet(name) { return "Hello, " + name + "!"; }const multiply = function(a, b) { return a * b; };const square = (n) => n * n;const sayHello = (name = "World") => `Hello, ${name}!`;`Hello, ${name}!`Review feedback below
person - Objects are collections of key-value pairs enclosed in curly braces {}. Create a person object with properties: name (string), age (number), city (string), and isEmployed (boolean).const person = {
name: "John Doe",
age: 30,
city: "New York",
isEmployed: true
};person.name to get the name. Print at least two properties using dot notation.person["age"]. Bracket notation accepts strings or variables: const key = "city"; person[key].person.email = "john@example.com" or person["phone"] = "555-1234".greet method that returns a greeting using this.name. The this keyword refers to the current object.person.greet = function() {
return `Hello, my name is ${this.name}`;
};Object.keys() to list all properties - Object.keys(obj) returns an array of the object's own property names. Useful for iteration and inspection.Object.values(obj) returns an array of property valuesObject.entries(obj) returns an array of [key, value] pairs{ name, age } instead of { name: name, age: age }const { name, age } = person;const copy = { ...person };this (arrows don't bind their own this)Person object: { name: 'John Doe', age: 30, city: 'New York', isEmployed: true }
Name (dot notation): John Doe
Age (bracket notation): 30
After adding email: john@example.com
Greeting: Hello, my name is John Doe
Object keys: [ 'name', 'age', 'city', 'isEmployed', 'email', 'greet' ]
const person = { name: "John", age: 30 };person.nameperson["name"] or person[variableName]person.email = "john@example.com";person.greet = function() { return this.name; };Object.keys(person) returns array of property namesReview feedback below