What is eval function in javascript - Where is it used?

I know less about eval function and heard some javascript developers saying that it should not be used in modern code, because it can perform unexpected things in your code since it receives code and evaluates them for your needs Make me understand the concept

»Javascript @aksarah

» 2 answers  » 109 views   

Add photos

Gadrawin • 2 months ago
This built-in function eval() is old and rarely used in modern programming especially javascript because long time ago programming languages were much weaker where many things could only be done with eval because of sometimes fear of writting bad codes and many reasons. but it is still used. for example: * When you are making simple calculator (100 + 6 / 3) * When you want to bypass webpack's compilation process * When you are developing template libraries * Use it when you are dealing with interpreters Simple usage in calculator: const displayIn = document.getElementById("display"); displayValue = 0; evalstorage = []; evalstorage.push(displayValue); let evaluated = eval(evalstorage.join(" "); displayValue = evaluated + ""; displayIn.innerText = displayValue; Code explanation: in variable display i get where to display result, and declare evalstorage which is empty array, I'll push 0 value in that array, and when someone entered operator or number I'll get and keep them in displayIn in order to have format like this 5 / 4 (as most calculator do) we used variable evaluated to turn it from array like this ['5', '/', '4'].  »

0  Dislike  •  Reply

Devuntildeath • 2 months ago
Eval is a global function takes your code string which contains function declarations, variables, breaks, etc. and evaluate it for you. as it's clearly means "evaluate" Generally you can do many things without using val, even though it can dynamically load you javascript code as strings and it evaluates it for you. // Example: use of arrow function inside eval let simpleFunction = eval( '(num1, num2) => (num1 + num2)' ); alert(simpleFunction(3, 5)); // 8 The code in eval function only see inner variables (variables which is in eval function), Therefore, the inner variables can't confuse with outer variables because eval function only see its variables). example: let num = 5; function getResult() { let num = 10; eval('alert(num)'); // it will give 10, doesn't care about global ones } getResult(); Again, after reusing that inner variable, it will change the outer variable(that one declared before). #You can use eval in global scope by using window.eval(code). Thats simple. look at simple example let a = 6; { let a = 8; window.eval('alert(a)'); } You will get expected result (6) because it is executed in global scope. # What if your eval'ed code needs local variables? In order to be used locally, you will need to change it with new Function and pass those eval'ed values as arguments because "new Function" is there to create a function from string. if you wish to know many about it Read this StackOverFlow's post. »

0  Dislike  •  Reply

Back to top

[SHARE THIS POST]: The post's archived Spread it to others!

Posted under this donnektish »
This community is about sharing contents such as questions and answers discussing about Javascript and TypeScript, ES5, ES6
62 people joined this donnektish

Maintain social distancing, to avoid Covid19