String evaluation function in JavaScript [duplicate] - javascript

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.

Related

Meaning of exclamation before dot [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 7 months ago.
I've seen somewhere written in the code fixture.componentInstance.dataSource!.data = [];
I want to know the meaning of dataSource!.data
I know about the meaning of question mark(?) before dot(.) like key1?.key2 but an Exclamation(!) before dot(.) !. expression makes me curious.
Many thanks in advance!!
It's called the Non-null Assertion Operator
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-
It removes null and undefined, allowing you to assert that you know there will be a value. Usually this is a code smell, something that should give you pause to consider other methods so that you can avoid using it. Type narrowing would be preferred. But in some specific instances, this operator can be a useful tool.

If statement from string statement [duplicate]

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.

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.

difference between if (!!variable), and, if (variable)? [duplicate]

This question already has answers here:
Can someone explain this 'double negative' trick? [duplicate]
(9 answers)
Closed 9 years ago.
Is there any difference (in JS) between using the double negative !! and not using it at all?
For example
if (!!variable){... vs. if (variable){...
I know there are times where I've gotten a warning using the 2nd method..
When should each be used? and when will each throw a warning in the console? (for variables, objects, arrays etc.)
Thanks!!
There is a difference for assigning it, but not for using it in a conditional statement. The reason the !! is used is because the first ! will convert your variable to its truthy evaluation and then not it. So "hello" becomes true, is then negated, becomes false, and the second ! will negate the false, resulting in true. This can be desirable when trying to obtain the thruthy value from a variable. However, there is not much gained by doing it in an if statement.
In this particular case, there is no difference. In fact, !!variable is wasteful.
However in more general cases, it casts the variable to a boolean. Personally I've only found this useful when debugging, and to learn what values are truthy and falsy.

Usage of toString in JavaScript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 9 years ago.
I'm reading through Douglas Crockford's JavaScript: The Good Parts, and I'm at the point where he defines a fade function. Part of this code boils down to this:
var level = 1;
var hex = level.toString(16);
So I run this in my browser's console to see what I get....
var level = 1;
level.toString(16);
Hey, it returns "1"... Fabuloso! Wunderbar!
Then to be cheeky, I try this to see what I get...
1.toString(16);
And I get
SyntaxError: Unexpected token ILLEGAL
What the what? If level is a variable equal to 1, and running this method on level works fine, then why doesn't running this method on the actual number 1 work? I tried a similar experiment with the toPrecision() method and that worked fine in both cases. What's the issue here? Is this another one of those inherent flaws in the JavaScript implementation, or am I missing something? I am testing in Google Chrome.
Related: Stack Overflow question Why don't number literals have access to Number methods?.
It's just a language grammar limitation.
Since 1. is a legal literal number (and 1.t is not) the tokeniser will split this into the following tokens:
1.
toString
(
)
And that's an illegal sequence of tokens. It's object method, instead of object . method.
In the working versions in #Joey's answer, the braces prevent the tokenizer from treating the dot as part of the number literal instead of as a separate token, as does writing:
1.0.toString()
or
1..toString()
since the tokenizer knows that the second dot must be a token on its own, and not part of the number literal.
You need 1..toString or (1).toString to get the number literal
level is a variable (and thus an object).
1 is a literal. They are not objects and the interpreter thinks about them completely differently.
http://www.cs.brown.edu/courses/bridge/1998/res/javascript/javascript-tutorial.html#4

Categories

Resources