Nullish Coalescing Operator In JavaScript In Hindi
Comma operator In JavaScript In Hindi | JS Comma operator In Hindi
JavaScript Object keys, values, entries : Object methods in JS
JavaScript Spread syntax In Hindi : Spread Operator In JavaScript
Promise In JavaScript In Hindi | JS Promise constructor
Back end developer roadmap : how to became back end developer ? back end development kya hai?
What is OSI Model In Hindi | Layers of OSI model
How to setup MongoDB In Laravel : Laravel with MongoDB
PHP Null Coalescing Operator In Hindi , Null Coalescing Operator kya hai ?
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.
JS eval()
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