This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I was just trawling through the QUnit source code (1.12.0) and came across a line that confused me. I've done a bit of googling and haven't been able to come up with a reason for it.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 520
result = !!result;
A similar thing appears further on in the code, except instead of storing the result in itself, it's storing the double negated variable in JSON.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 957
result: !!result
As ! negates, I assume !! will negate then negate again, thus ending up with exactly what you started with. In which case, what is achieved by setting a variable equal to itself, negated twice? (Or in the latter example, returning itself negated twice instead of just returning itself.)
!! is used to convert the value to the right of it to its equivalent boolean value.
Also check this related Thread.
Related
This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 3 years ago.
code snippet of unirest
As you can see above, arrayIndexOf refers to a function. When invoking the arrayIndexOf function,
is it necessary to put "~" before "arrayIndexOf"? I try to change "~arrayIndexOf(value, field)" to "arrayIndexOf(value, field)". And it works as the same? Is there anything I miss?
In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right
before an indexOf() to do a boolean check (truthy/falsy) on a string.
On its own, indexOf() returns the index number of a String object
passed in. So if -1 is returned it will be turned into 0 which is
falsy.
Source: https://wsvincent.com/javascript-tilde/
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
I know 1 is not an object, but when I type 1..toString(), it returns "1" in the console. Why is that?
Because the JavaScript parser assumes that 1. must be followed only by one or more digits to represent a float number. Using parentheses works: (1).toString().
Because it is interpreting 1. as the number. When you have 1.toString(), it is the same as saying (1.)toString(). Therefore 1..toString() is the same as (1.).toString()
The reason why the below works is:
1..toString()
The 1.. is considered as a floating point number. The console expects something like:
1.0
1.5
Or something. If you are giving something like:
1.toString();
The above is not a valid number. That's the reason. So to make the above work, you need a parenthesis to tell that the number is completed:
(1).toString();
This question already has an answer here:
(![]+[])[+[]]... Explain why this works
(1 answer)
Closed 8 years ago.
I realize that this is intended as a joke, but someone posted this and it alerts "fail" in the browser, what is going on that makes this happen?
alert((![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]);
Here is a good blog post about this topic: http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html
You basically create strings ("false","undefined" and others) and again use addition of true's to get the indexes.
For example the first letter (f):
(![]+[])[+[]]
You use ![] to get false, and add [] to convert it into a string. The content within the parenthesis is now "false". You then access it like an array, and ยด+[]` converts to 0 (false as a number).
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I access a property of an integer with a single dot?
I was reading an article, and came across the strange behaviour of javascript toFixed method. I don't understand the reason for the last statement. Can anyone explain please?
(42).toFixed(2); // "42.00" Okay
42.toFixed(2); // SyntaxError: identifier starts immediately after numeric literal
42..toFixed(2); // "42.00" This really seems strange
A number in JavaScript is basically this in regex:
[+-]?[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?
Note that the quantifiers are greedy. This means when it sees:
42.toFixed(2);
It reads the 42. as the number and then is immediately confronted with toFixed and doesn't know what to do with it.
In the case of 42..toFixed(2), the number is 42. but not 42.. because the regex only allows one dot. Then it sees the . which can only be a call to a member, which is toFixed. Everything works fine.
As far as readability goes, (42).toFixed(2) is far clearer as to its intention.
The dot is ambiguous: decimal point or call member operator. Therefore the error.
42..toFixed(2); is equivalent to (42.).toFixed(2)