This question already has answers here:
JavaScript: difference between a statement and an expression?
(11 answers)
JavaScript: declarations vs expressions vs statements
(3 answers)
What is the difference between an expression and a statement in JS?
(2 answers)
Closed 18 days ago.
I have seen a bunch of articles talking about "what is an expression in Js?" but some of them said that "an expression in Js is any piece of code that produces a value" like this one 4 + 4, so this one is of course an expression the value that this will produce is 8, but what about if we only a single value of type:x, like this: 4, they also said that this is an expression but this really doesn’t match or isn’t too accurate with the previous definition, and the other case is that if we do this x = 2, that is considered also an expression an if we move forward x is also an expression, so, what really is an expression? is there different types of expression in Js? which ones are this? what are they differences? what is the really accurate definition for this concept?, please someone with this knowledge I really appreciate if you can clarify this to me!
Related
This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!
This question already has answers here:
Match exact string
(3 answers)
What is the MM/DD/YYYY regular expression and how do I use it in php?
(9 answers)
Closed 4 years ago.
I am trying to learn how to use regular expressions. Currently, I am creating my own regular expression using JavaScript to test for a date in the format of MM-DD-YYYY.
Here is my code:
// regex for testing valid date
var regex = new RegExp("[0-9]{2}\-[0-9]{2}\-[0-9]{4}");
regex.test("113-12-1995");
Unfortunately, this is outputing to true and I cannot figure out why. I am under the impression that {2} means it must be two digits and no more or less. It seems like it is behaving as if I had put a {2,} which would correlate to at least two digits, but that isn't what I want.
Additionally, how would I test to see if the value of the first two digits are greater than 12?
This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
What's going on here using JavaScript?
var c = +(3,13);
I got c equals to 13. Why?
The comma functions as an expression separator operator. The value of a sequence of expressions separated by commas is the value of the last expression. Thus 3, 13 has the value 13. That's surrounded with parentheses and the unary + operator, neither of which will affect that value.
The comma operator is useful in only a small number of situations, generally involving expressions with side effects. In particular, your example is basically pointless. However, there are situations where some statement syntax allows for a single expression, so the comma operator lets you sneak in more than one.
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Maybe I am too quick in asking this question , but I was going through angular code and I found the logical expression evaluation like this :
https://github.com/angular/angular.js/blob/master/src/ng/directive/attrs.js#L362
Essentially an attribute was evaluated like :
attr.$set(attrName, !!value);
Is there a particular reason why was this done this way ?
!! is a concise way of ensuring that value will be a boolean.
This question already has answers here:
Evaluating a string as a mathematical expression in JavaScript
(26 answers)
Closed 8 years ago.
I am trying to create a simple graphing calculator where a user enters a function of f (like f(x) = x^2+2x+6). Basically the javascript replaces the x in the function with some number and then evaluates the function using eval(). The problem is, I want users to be able to type x^2 instead of default javascript which would be Math.pow(x,2). I'm guessing it's going to be some regular expression but I have little experience with them and find them really confusing, personally. Is it possible to convert a statement like x^3-x^2 to Math.pow(x,3)-Math.pow(x,2) ??
Help greatly appreciated.
You want to use a Regular Expression that looks something along the lines of
(.+)\^(.+)
This will match both selections, you then replace the instances of that string, using those matches like this.
Math.pow($1, $2)
Javascript has support for this kind of operation with the function option in String.prototype.replace
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
1) Yes, it is possible. You can easily program it yourself if you parse what the user entered. Wherever you see x^n just turn it into Math.pow(x,n). And only then eval.
This will work for say polynomials of one variable.
2) If you want to solve this more generally (for a broader class of math functions),
you need to come up with some grammar and build an AST from the user input.
http://en.wikipedia.org/wiki/Abstract_syntax_tree