Exercise 6: Adding Numbers with JavaScript
Learn how to capture values and add them using JavaScript. Perfect for getting started with manipulating numerical data in forms or web interfaces.
The main objective is to learn how to use arithmetic operators and work with numbers.
Activity
-
Declare two variables called num1 and num2 and assign them numerical values.
-
Use the addition operator (+) to add both numbers and store the result in a variable called result.
-
Display the result 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>Sumando Números</title>
</head>
<body>
<h1>Ejercicio 6: Sumando Números</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 numéricas
let num1 = 10;
let num2 = 15;
// Sumar los números
let resultado = num1 + num2;
// Muestra el resultado en la consola
console.log("El resultado de la suma es: " + resultado);
info
🔎Check the sectionBasic JavaScript Operatorswhere you will find more information on this topic.