Skip to main content

Exercise 3: Greeting with Variables

Discover how to use variables in JavaScript to personalize messages and greet users dynamically. A practical exercise to master the basic syntax of variables.

The goal is to learn how to declare variables in JavaScript and use them to store and display information in the console.

Activity

  1. Declare a variable called name and assign your name to this variable.

  2. Use console.log() to display a greeting in the console, including the value of the name variable.

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>Saludando con Variables</title>
</head>
<body>
<h1>Ejercicio 3: Saludando con Variables</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 la variable y asignación de valor
let nombre = "Ana";

// Muestra el saludo en la consola
console.log("¡Hola, " + nombre + "! Bienvenido a la programación.");
info

🔎Check the sectionConcepts about variables in JavaScriptwhere you will find more information on this topic.