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
Easy- Write a function calculateResult to conclude the calculation after clicking on the ‘=’ button.
- Set the attribute onclick to calculateResult() for the ‘=’ button.
- 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
1Introduction
What will you build?4Make it work
function appendToDisplay