This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 8 months ago.
function expect(argument) {
this.eat(argument) || this.undefined();
}
Can someone please help me explain how this code works... the question is focused on this.eat(argument) || this.undefined();. I am guessing that one of those methods would run? and if it is, how do I tell which would run
Or Condition || will return the first true value.
if this.eat(argument) return true or any value which can be converted to true value, So the this.undefined() function will not execute.
If not it will execute.
Related
This question already has answers here:
Why [] == [] is false in JavaScript?
(6 answers)
Weird behavior of comparison operator JavaScript when using empty array [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to understand why this simple code is saying false instead of true.
let test1 = JSON.parse("[]");
let test2 = JSON.parse("[]");
console.log(test1 == test2);
Surely this code should be true but I always get it saying false. Whats the issue?
Thanks
This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
Python programmer here.
I don't know how to write this. I tried using 'if !in' and '!if in', but I don't know how. Tried to Google it but got no results.
The correct syntax is
if(!condition){
expression();
}
Note that you need parenthesis around the condition.
#plalx wants a formal definition, and here you go:
IfStatement:
if(Expression) Statement else Statement
if(Expression) Statement
In case of any ambiguity the else would be matched with the nearest if.
If you have some value:
var excludeMe = "something unwanted";
Then you can use the following if statement:
if(myTestCase !== excludeMe) { //do something...}
Keep in mind that != does not check type and !== does check type. So, 1 != "1" is false and 1 !== "1" is true.
This question already has answers here:
What is an exclamation point in JavaScript?
(2 answers)
Closed 7 years ago.
I was wondering what does the ! actually mean in this method
$scope.toggleSelected = function () {
$scope.selected = !$scope.selected;
};
I understand it's allowing me to set a selected item and it won't work without it but what exactly is the ! for?
The ! is the normal negation operator.
Inside of that function, it's used to flip/toggle the value each time it's called. For example, from true to false and vice-versa.
This question already has answers here:
How is this valid javascript, and what purpose does it serve?
(2 answers)
Closed 7 years ago.
I'm switching from C++ to JS.
//if ( condition )
{
instruction1;
instruction2;
}
In C/C++ commenting if before the block would simply execute the block unconditionally.
Could I use it the same way in JS? Or would it create an unnamed object which is never used?
A very quick way of testing:
{
alert("foo");
}
http://jsfiddle.net/4obksb0s/
Yes - it appears to run fine.
This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 9 years ago.
i have written my code something like this
this._users = users || [];
just wanted to know what does this mean to this._users?
Thanks for the help.
This basically is evaluating users to true or false. If it evaluation is true, than return users otherwise, assigns an empty array to this._users .