If statement from string statement [duplicate] - javascript

This question already has answers here:
What's the main benefit of using eval() in JavaScript?
(15 answers)
Closed 4 years ago.
I would like to ask if it's possible to write an if-statement using a string and convert it somehow into a real statement?
I wish I could use "c==c" like if(c==c).
Is that possible?
var c=1;
var aa= "c==c";
if(aa) {
console.log("abc")
}

Yes, use Javascript's eval function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Eval let's you run any string as valid Javascript code. However eval has a few downsides like performance and security since this can lead to vulnerabilities in your code if you let users eval any string they want.

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

How to see the code of a javascript function? [duplicate]

This question already has answers here:
How can I read ‘native code’ JavaScript functions?
(3 answers)
Closed 3 years ago.
This may be a very dumb question.
I want to see the code of a function (Built in and User defined) in Javascript.
For example :
function hello(){
console.log("hello")
}
hello.toString() // Gives the function definition
'function hello(){\nconsole.log("hello")\n}'
Is there a way to see the native code like Math.random.toString()?
Update: From the comments, Seblor explained that native code cannot be seen.
You could do some string formatting to get a "better" look at your functions. Use this peace of code to get rid of the function name to get just the code.
function justGetCode(funcName)
{
var tempString = funcName.toString();
tempString = tempString.substring(tempString.indexOf("{"));
return tempString
}
But beyond this there is little you can do in terms of digging into native (i.e. browser specific ) code as it is encapsulated. This should work on library functions however.
Now i do not know what you are planning on doing with the returned function, but for fancier function manipulation you can always use in-built reflection mechanisms

Javascript use variable name to thread a function? [duplicate]

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

do I need eval? [duplicate]

This question already has answers here:
Why does eval() exist?
(6 answers)
Closed 9 years ago.
I've searched the web for what eval does and here is what I have found:
The eval() method evaluates JavaScript code represented as a string.
And I've read this question, which says that it is evil.
But I really don't understand what does it do, i.e I don't see when to use eval.
I mean:
var x= 3;
var y =5;
var z = eval("x+y");
// is the same as:
var z = x+y;
so as I see it's just adding characters to my code. Can somebody give me an example of why eval was created in the first place?
It allows execute dynamicly generated code, not just "x+y".
Eval allows you to evaluate a string as javascript code. This means you can do things like evaluate a string to call a function:
function add(x,y){
return (x+y);
}
stringToEval = 'add(5,6)';
eval(stringToEval);
This code would run the function by evaluating the string as a function call.
So I would say you do not need to use eval here. Just add the two variables.

String evaluation function in JavaScript [duplicate]

This question already has answers here:
How to convert string equation to number in javascript?
(5 answers)
Closed 8 years ago.
Is there any built-in function in JavaScript like eval built-in function in Python?
notice: eval function take an equation as string and returns result. for example assume variable x is 2, then eval("2x+5") returns 9.
Yes, there is eval function in JavaScript too.
Besides, the statement should be valid for evaluation, i.e. eval("2*x+5").
You should also note, that using eval in JavaScript is not recommended. You can read about at MDN.
REF: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval
Yes, it's called eval, oddly enough.
http://www.w3schools.com/jsref/jsref_eval.asp
Edit: the mozilla link from VisioN (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval) is better, since it explains why you shouldn't use it.
You can use eval() however you will need to convert any "human" math operators to ones understood by javascript.
var sum = "4x5";
eval(sum.replace("x", "*"));
Update didn't read the part about x being a variable! that does make it slightly more complicated depending on how complex the equations are.

Categories

Resources