Menu
Coddy logo textTech

function calculateResult

Lesson 14 of 14 in Coddy's DOM Project - Calculator course.

Eval function

In JavaScript, the eval function is used to execute a string of JavaScript code. This function takes a string as an argument and treats it as if it were a line of code, evaluating it within the current scope. Here’s a detailed explanation of how eval works, along with some examples:

Syntax:

eval(string)

Example:

let x = 10;
let y = 20;
let result = eval("x + y");  // Evaluates to 30
console.log(result);  // Outputs: 30
challenge icon

Challenge

Easy
  1. Write a function calculateResult to conclude the calculation after clicking on the ‘=’ button.
  2. Set the attribute onclick to calculateResult() for the ‘=’ button.
  3. Refresh the web and test it!

Try it yourself

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
</head>
    <body>
        <!-- Write HTML code here -->
        <script src="script.js"></script>
    </body>
</html>

All lessons in DOM Project - Calculator