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.
Related
This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.
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.
This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 5 years ago.
This one is probably fairly straightforward, but I haven't succeeded in finding an answer as of yet. Anyway, what I want to do is compare one value against a group of others, and run the script if it matches one of them. Here's the verbose version of I want to do, which works fine, but I'm assuming there's a smarter, less verbose way to achieve this?
function updatePosition(e) {
if((e.target.innerHTML == 1) || (e.target.innerHTML == 4) ||
(e.target.innerHTML == 7)) {
console.log(e.target.innerHTML)
}
}
You can store all the valid comparison inside an array. Then you can use Array#Find() to get a matching value. If there is no matching value, you will get undefined
function updatePosition(num) {
let validNumbers = [1,4,7];
console.log(validNumbers.find(valid => valid === num));
}
updatePosition(4);
updatePosition(9);
This question already has answers here:
What is "?:" notation in JavaScript?
(5 answers)
Closed 6 years ago.
Can someone interpret this javascript line for me?
mouseWheelEventName = $.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel',
Need to know what "?" does, and what 'DOMMouseScroll' : 'mousewheel', is saying particularly the "," at the end of the line... why isn't it a ";"
Thank you.
This is a ternary operator, used as a shorthand conditional statement:
it's the same as saying:
if ($.browser.mozilla) {
mouseWheelEventName = 'DOMMouseScroll';
} else {
mouseWheelEventName = 'mousewheel';
}
The first piece before the = is declaring a variable (mouseWheelEventName) dependent on the following condition.
The next piece between = the ? is the condition (is $.browser.mozilla true?).
Immediately after the ? is the then portion (if the condition is true, set the variable mouseWheelEventName to the string DOMMouseScroll).
After the : is the else (if the condition is NOT true, set the variable mouseWheelEventName to the string mousewheel).
Further reading:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
As for why there is a comma at the end of it, we'd need to see a more complete code sample including what follows that to say for certain. If I had to guess, I would say the author of the code was chaining variable statements. This answer may shed some light for you: Javascript best practices, why to use comma to chain function/variable declarations? (see chosen answer)
This question already has answers here:
Javascript if in x [duplicate]
(3 answers)
Closed 6 years ago.
Hi i'm following a tutorial learning to use javascript.
function move(keyclick) {
if(40 in keyclick) {}
playerMove.x++;
render(); }
What does the 'in' word mean? I understand what the function is doing, but why not just use ==
?
Thanks
The in operator is true if the string on the LHS is the name of a property that exists on the object on the RHS.
== tests if a value matches another value, which is entirely different.
The in operator returns true if the specified property is in the specified object (cited from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).