Exercise 5: Concatenating Personal Information
Learn how to combine multiple variables to form personalized messages with data such as name, age, or city. Practical exercise in concatenation in JavaScript.
The main objective is to practice combining variables and displaying a complete message in the console.
Activity
-
Declare three variables called name, city, and hobby.
-
Assign a value to each variable representing your name, the city where you live, and a hobby you enjoy.
-
Create a variable called message that combines the three variables into a complete sentence.
-
Display the message in the console.
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>Concatenando Información Personal</title>
</head>
<body>
<h1>Ejercicio 5: Concatenando Información Personal</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
// Declaración de variables
let nombre = "Ana";
let ciudad = "Buenos Aires";
let hobby = "leer libros";
// Creación del mensaje combinando las variables
let mensaje =
"Hola, soy " + nombre + ", vivo en " + ciudad + " y me gusta " + hobby + ".";
// Muestra el mensaje en la consola
console.log(mensaje);
info
🔎Check the sectionBasic JavaScript Operatorswhere you will find more information on this topic.