JavaScript fetch a specific value with eval()?


The eval() is used to evaluate an argument in JavaScript. Following is the syntax −

eval(str)

Here, str can be an expression, variable or statement(s).

Let’s say the following is our array −

var studentMarks = [76,34,56,78,81,98,90,59,64];

Now, fetch a specific value with eval(). Here, we are fetching the value at index 1 −

Example

var studentMarks = [76,34,56,78,81,98,90,59,64];
var stringValue = eval("studentMarks");
console.log(stringValue[1]);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo80.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo80.js
34

Updated on: 07-Sep-2020

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements