An object in JavaScript is a collection of key-value pairs, where each key is a string and each value can be any valid JavaScript data type, including another object.
let user = {
name: "Anshuman",
age: 19
// key:value,
};
console.log("Anshuman's age is " + user.age);
1️⃣ Write a function that takes a user object as input and greets them with their name and age.
2️⃣ Write a function that takes an object with name
, age
, and gender
, and greets the user accordingly.
(Example: "Hi Mr./Mrs./Others Anshuman, your age is 21")
3️⃣ Modify the function to also tell the user if they are legally allowed to vote (age > 18).
Arrays allow you to group multiple values together.
const users = ["Anshuman", "Raman", "Diljeet"];
const totalUsers = users.length;
const firstUser = users[0];
console.log("Total users:", totalUsers);
console.log("First user:", firstUser);
1️⃣ Create an array of objects, where each object contains name
and age
.