JavaScript "new Function" Syntax : JS "new Function" Syntax
async - await In JavaScript In Hindi | JS async - await In Hindi
Calculator In Jquery In Hindi | jQuery Calculator In Hindi
Promise In JavaScript In Hindi | JS Promise constructor
JavaScript Destructuring Assignment In Hindi
Linux adduser Command In Hindi
Selection Sort in PHP With Example | How does selection sort work in PHP ?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
eval()
function , as a String pass किये गए JavaScript code को evaluate करके result return करता है , eval()
function में pass की गयी String को as a script parse / evaluate होती है।
for example
console.log(eval('2 + 2'));
console.log(eval('4 * 5'));
console.log(eval('10 / 5'));
4 20 2
अब हालाँकि आप जानते हैं कि JavaScript में String को कई तरह से define करते हैं तो आप , single quote string की जगह double quotes , back ticks या String function का use भी कर सकते हैं।
console.log(eval("2 + 2"));
console.log(eval( String(`10 % 3`) ));
console.log(eval(`10 / 5`));
4 1 2
ध्यान रहे कि example में String function का use किया गया है String object का नहीं , eval()
function में String object work नहीं करेगा।
console.log(eval( new String("2 + 3") ));
String { "2 + 3" }
normal statements की तरह ही आप eval() function में भी variables को use कर सकते हैं या उन पर कोई calculation कर सकते हैं।
for example
let x = 20;
let y = 10;
console.log(eval( 'x'));
console.log(eval( 'y'));
console.log(eval( 'x*y'));
console.log(eval( 'x-y'));
20 10 200 20
बैसे तो eval()
function में pass किये गए data के according , completion value return
होगी लेकिन अगर evaluation के बाद completion value empty होती है तो undefined
return होगा।
console.log(eval("")); // undefined
eval() method को अभी तक आपने directly string में data pass करके call किया है है , हालाँकि इसे indirectly भी कई तरीके से use किया जा सकता है।
let x = 20, y = 10;
// Indirect call using the comma operator to return eval
console.log((0, eval)("x + y"));
// Indirect call through optional chaining method.
console.log(eval?.("x + y"));
// Indirect call using a variable to store and return eval.
const geval = eval;
console.log(geval("x + y"));
// Indirect call through member access.
const obj = { eval };
console.log(obj.eval("x + y"));
Output
30 30 30 30
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook