My .NET backend handle enum authorization with HasFlag()
Enum Foo {
0: Zero,
1: One,
2: Two,
3: Three,
..
}
5.HasEnum(Foo.One) is falsy because 5 is equal to 3 + 2
6.HasEnum(Foo.One) is truthy because 6 is equal to 3 + 2 + 1
How to handle it in JavaScript ?
I'm receiving a rôle (that is > 0 and < 1048), how to know if it has 32 rôle ?
Related
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 1 year ago.
I am new to javascript, and I tried to run the below in Brave browser:
'1' + '2' - 3;
The browser replied with the value 9, which I don't understand.
'1' + '2' is concatenation of strings and result is '12'.
'12' - 3 is math operation 12 - 3 and result is 9.
You shoud do the intermediate steps and see for yourself.
Try:
a = '1' + '2';
console.log(a);
b = a - 3;
console.log(b); // prints 9
The + operator is described as for purpose of sum of numeric operands or string concatenation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition
That is because your first two numbers are strings. you would get a correct answer if you used:
1 + 2 - 3
Or if you cannot avoid doing a calculation on a string, convert it to a number first:
parseInt('1') + parseInt('2') - 3
'1'+ '2' = '12' is a string but it is converted to an integer before being substracted by -3, so 12-3.
This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
As we know about the greater than and less than operator that how they work.
console.log(2 < 12) // true
console.log(2 < "12") // true
console.log("2" > "12") // true
But when we compare more than 2 values with greater than and less than operator. Then I can't understand the logic behind the scene.
1. console.log(1 > 2 < 3) // true and console.log(1 > 2 > 3) // false
2. console.log(9 > 2 > 9 < 8 < 9) // true
3. console.log(9 > 8 > 7 > 9 > 5) // false
Can somebody please elaborate and explain behind the reason for 1 , 2 and 3 ?
I'd like to clear this problem using following example.
1 < 2 < 3 // evaluates to true
But
3 > 2 > 1 // evaluates to false
As we know js execute from left to right. Therefor, in first example when statement
1 < 2
is executed it evalutes to true. In programming true refers to 1 and false refers to zero. Then after executing the above statement we have true means 1. Now when this result is combined with next statement
true < 3
Then in short this statement means
1 < 3
because true refers to 1 in programming. Since 1 < 3 is true that's why we have the final result as true.
Coming to the next example 3 > 2 > 1 which evaluates to false because
3 > 2 // evaluates to true
Since above statement is true then in programming sense we got 1(true) and then by combining this result with next statement
true > 1 // evaluates to false
The above statement is in short
1 > 1
Since 1 is not greater than 1 therefor it returns the final result to false.
3 > 2 = true
true > 1 = false
So finally we conclude that by programming sense both statement evaluates to different results but in sense of mathematics both are same. Its a interview question. Hopefully you understand this.
Please let me know if needs any other information. In other case, It might be accepted answer.
Why when I run in Chrome console this snippets of code I get different results?
1 < 2 < 3
true
3 > 2 > 1
false
Javascript doesn't support expressions like 0 < x < 10. Because the < and > operators have equal precedence and are applied left to right, in effect what is being evaluated is (1 < 2) < 3.
Because the expression (1 < 2) evaluates to true, the second < operation is, in effect, evaluating whether true is less than or equal to 3.
1 < 2 < 3
(1 < 2) < 3
true < 3
true
Your second example can be understood by applying the same procedure:
3 > 2 > 1
(3 > 2) > 1
true > 1
false
As for why true < 3 evaluates to true and true > 1 evaluates to false, the answer is a bit more complex, and has to do with the loose typing system in Javascript. In short, when compared with a number, javascript's true value evaluates to 1, and its' false value evaluates to 0.
This chapter on the substitution method of procedure application is fairly helpful. https://mitpress.mit.edu/sicp/full-text/sicp/book/node10.html
Because programming is not math.
1 < 2 returns boolean true which is then implicitly converted to a number 1 which is indeed less that three. Same thing happens in the second example but 1 > 1 returns false.
I am looking at a more elegant way to match and either or in JavaScript.
With strings, we can do the following:
"test".match(/test|otherstring/)
But the number object has no method match. We can of course add this, but my application will be dropped into a large international site with hundreds of separate applications, so this would be a dangerous move.
At this point I have two options:
String(myNumber).match(/test|numbers/)
Or:
if(number === test1 || number === test2)
But neither of these solutions seem very elegant to me. What I would like is to be able to do:
number === test1||test2;
But the result of this is either true if number===test1 of if not it returns test2. Or use brackets:
number === (test1||test2)
But this only compares the first test and returns the result of that.
Here we can have a little more fun with JavaScript wierdness. Bonus points for anyone who can tell me why the following sequence occurs:
13===(13|13)
# true
13===(12|13)
# true
13===(1|13)
# true
13===(133|13)
# false
13===(13|133)
# false
for this question
13===(13|13)
# true
13===(12|13)
# true
13===(1|13)
# true
13===(133|13)
# false
13===(13|133)
# false
A single pipe is a bit-wise OR.
example
alert(6 | 10); // should show 14
8 4 2 1
-------
0 1 1 0 = 6 (4 + 2)
1 0 1 0 = 10 (8 + 2)
=======
1 1 1 0 = 14 (8 + 4 + 2)
This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
In one JS library I saw such syntax:
if (val > 5 == t) { ... }
I tested this in console:
1 == 1 == 2 // false
2 > 1 == 1 // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3 // true
1 > 2 > 3 // false
At first glance all correct. Can this be used?
1 == 1 == 2 // this
true == 2 // becomes this
1 == 2 // which becomes this, and is false
2 > 1 == 1 // this
true == 1 // becomes this
1 == 1 // which becomes this, and is true
...and so on.
If you're wondering about the conversion, you should do a search on the == operator, which uses the Abstract Equality Comparison Algorithm.
You not really comparing what you think you are comparing:
(1 == 1) == 2 // actually true == 2 which is false
(1 == 2) == 1 // actually false == 1 which is false
Which is why strict equality === will fail in all cases
Javascript does not support the chained comparison syntax used in mathematics:
1 < 2 < 3 // 1 is less than 2 which is less than 3.
Instead, it evaluates each comparison left to right, which can sometimes yield the same results as mathematical chained comparison, as do all your examples, but the process is different:
1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
// Javascript returns true.
3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
// Javascript returns true.
For this reason, it should be discouraged.
To answer your question, however, yes it can be used.
It's a correct syntax but not one I would recommend. What's happening, is probably:
if ((val > 5) == t) { ... }
I tested this in console:
(1 == 1) == 2 // false
(2 > 1) == 1 // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3 // true
(1 > 2) > 3 // false
With the boolean on the left implicitly converted to an int.
There is nothing to stop you from using such an adventurous syntax however do keep in mind that one of the most frequent reasons for having bugs inside JavaScript code is messing up operator precedence.
Therefore its strongly advised to explicitly define precedence by adding brackets to precedence groups in case they consist of more than simple mathematical expressions for which the precedence is clearly determinable.
Another concern is type coercion.
jslint output:
Error:
Problem at line 2 character 13: Expected '===' and instead saw
'=='.
if (val > 5 == t) { }