Exercise 10: Creating a Person Object with JavaScript
Learn to build objects in JavaScript and group related data. This practical exercise teaches you how to model entities like a person.
The primary goal is to learn how to create a basic object and access its properties.
Activity
- Create an object called
personawith the following properties:
-nombrewith a value of your name.
-edadwith a value based on your age.
-esEstudiantewith a boolean value (for example,true either false).
- Display each property in the console using
console.log
Solution
Step 1: Create the index.html file
HTML
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Creando un Objeto de Persona</title>
</head>
<body>
<h1>Ejercicio 10: Creando un Objeto de Persona</h1>
<p>Los cambios se muestran en consola.</p>
<script src="script.js"></script>
</body>
</html>
Step 2: Write the code in script.js
JavaScript
// Crear el objeto persona
let persona = {
nombre: "Carlos",
edad: 25,
esEstudiante: true,
};
// Acceder y mostrar cada propiedad del objeto
console.log("Nombre: " + persona.nombre);
console.log("Edad: " + persona.edad);
console.log("Es estudiante: " + persona.esEstudiante);
info
🔎Check the sectionObjects in JavaScript: A Beginner's Guide with Exampleswhere you will find more information on this topic.