JavaScript eval() function17 Mar 2025 | 3 min read The eval() function in JavaScript is used to evaluate the expression. It is JavaScirpt's global function, which evaluates the specified string as JavaScript code and executes it. The parameter of the eval() function is a string. If the parameter represents the statements, eval() evaluates the statements. If the parameter is an expression, eval() evaluates the expression. If the parameter of eval() is not a string, the function returns the parameter unchanged. There are some limitations of using the eval() function, such as the eval() function is not recommended to use because of the security reasons. It is not suggested to use because it is slower and makes code unreadable. SyntaxValuesIt accepts a single parameter, which is defined as follows. string: It represents a JavaScript expression, single statement, or the sequence of statements. It can be a variable, statement, or a JavaScript expression. Let's understand the JavaScript eval() function by using illustrations. Example1It is a simple example of evaluating an expression using the eval() function. In this example, there are some variables. We are applying the eval() function on variables a, b, and c to calculate the sum, multiplication, and subtraction. Output After the execution of the above code, the output will be - 60 6000 -10 Example2In this example, we are calling a function using the eval() function. Here there is a function fun() having two arguments and returns the multiplication of both parameters. We are calling the function in the eval() function, and storing the result in the res variable. Output After the execution of code, the output will be - 2500 Example3 - Evaluate the string with JavaScript statementsIn this example, we are using the eval() function to evaluate the string with JavaScript statements. Here, there is string str, having JavaScript conditional if-else statement. We are matching the value of the variable 'x', if the value of x is 0, the output will be 'SUNDAY', else the output will be 'MONDAY'. Output After the execution of code, the output will be - The output is : SUNDAY Example - convert string to JavaScript ObjectsIn this example, we are converting the string to JSON object. Here, the string str contains the data as opposed to the code. We have to switch the data to JSON that allows the string to use a subset of JavaScript syntax for representing the data. Here, we are using the object obj to represent the data. Output After the execution of code, the output will be - Harry Rickman Next TopicKey-values-in-javascript |
JavaScript TextEncoder and TextDecoder are the functions to encode and decode the given value, respectively. The TextEncoder works to convert a specified string into the utf-8 standard using the JavaScript function. It transforms the string into a Uint8Array. Using a JavaScript function, the TextDecoder converts...
6 min read
In JavaScript, we can hide the elements using the style.display or by using the style.visibility. The visibility property in JavaScript is also used to hide an element. The difference between the style.display and style.visibility is when using visibility: hidden, the tag is not visible, but...
2 min read
What is a JavaScript file? JavaScript are files that contain the code for the execution on the webpage. JavaScript files are saved with the.js extension. Suppose you want to include JavaScript inside the HTML document you can use the <script></script> tag or you can include the...
4 min read
play a crucial role in managing asynchronous data in contemporary web and server-side applications. They enable developers to efficiently handle data flows by breaking down data into smaller pieces instead of processing them in large, memory-heavy chunks. This is why streams are especially useful for...
10 min read
Overview of the JavaScript Button The JavaScript button is one JavaScript element that adds effects to web pages. The website looks and feels good with JavaScript buttons. We can send and receive data using these JavaScript buttons, trigger click events, alter the text's color, and more. JavaScript...
4 min read
? A data type or class that consists of a grouping of several characters is called a string. We can handle and manipulate Strings using a variety of properties and methods that JavaScript offers. We can add, edit, and remove string content. Moreover, among numerous other items,...
3 min read
When it comes to web development, HTML, CSS, and JavaScript are the three most important languages. Having the correct Integrated Development Environment (IDE) may greatly increase productivity and optimize your workflow, regardless of the complexity of the web application you're developing or the simple webpage...
8 min read
Overview In JavaScript, an array is a global object type used to store data. An array is an ordered list or collection of zero or more data types that can be accessed by numbered indices beginning at 0. Because arrays allow us to store multiple values in...
5 min read
We will understand the in this article. Data structure and algorithm (DSA) are the backbone of the computer science and are important for solving difficult problems efficiently. They are associated with various programming languages such as C++, Java, Python, etc. Now, JavaScript programming language has...
18 min read
? The popularity of the Tor Browser has risen very rapidly over the last period of time. It was originally developed by the US Naval Research Lab in the mid-1990s to allow government officials to communicate in an anonymous and secure manner. Nowadays, most people tend to...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India