In Javascript how this pattern operates and by what name would you refer to it?
var valid = (value === value2 (car.color));
And how is different from:
var valid2 = if(value === value2){ car.color }
Those aren't even close to the same. In the first example, you have a function, value2(), which accepts a variable car.color as a parameter, returns something, which is then compared to value. It's not exactly clear that value2 is a function by the declaration or naming used in the code here, however, and the spacing doesn't help clarify it's purpose.
Your second example won't parse correctly, because you can't assign a variable to an If statement. If it were possible, it still wouldn't make sense, because if value == value2, you enter a code block where the only statement is not valid.
It appears that value2 is a function, so if value2 returns something that is equal to value then valid is true, otherwise false. Your second statement, var valid2 = if(value === value2){ car.color },
however, is invalid JavaScript.
Related
I have assigned my selected option to a variable. However, when I use my if statement to compare the variable and a string, the if statement executes the code regardless of if the variable is equal to my string.
I have tried rewriting the code and doing some research on javascript if statements but, to my knowledge, I am unaware of any errors in my code.
$(document).ready(function(){
$("#main_dropdown").change(function(){
let selectedMajor = $(this).children("option:selected").val();
if (selectedMajor = 'arts') {
alert("Your selected major is: " + selectedMajor);
}
});
});
You are attempting to compare them using = which will actually assign 'arts' to the selectedMajor variable. Use == instead which will compare them.
If you need to make sure the types are the same as well, use ===.
In javascript = is used to assign a value to a variable, == is actually the eqaulity check operator.
Also use trim() function to check the compared value doesn't contain any leading/trailing space.
Try changing,
if (selectedMajor = 'arts') {
as,
if (selectedMajor.trim() == 'arts') {
Hope this helps!.
Your if statement is using the assignment =. You want to compare with the ==.
selectedMajor = 'arts' is an Assignment which will always return the value on left hand side.
Simple assignment operator is used to assign a value to a variable. The assignment operation evaluates to the assigned value
let x = 3;
console.log(x = 3)
If you want to compare the value you should Abstract Equality == or Strict Equality ===
let x = 3;
console.log(x === 3)
[wasn't sure how to word this question exactly, but] say I have some code:
var myValue = 6;
var newValue = myValue == 6 ? myValue : 3;
notice that I'm using the ternary operator to check if a variable fulfils a certain condition, and if so, then I set my newValue to that variable, and if not, something else.
So the (slight) problem:
In a case like this, I have to actually write the variable name twice (myValue == 6 ? myValue : ...), this usually isn't a problem, but say I have a very long variable name, or even the property of a certain array or something else I don't want to manually write out, or even make a new variable; I just want to set the new variable equal to the value of the variable in the condition.
If my condition is simply if the expression exists, then I could obviously do:
newValue = myValue || 3;
but if I'm checking any other condition, I have to write the checking-variable twice, so is there any way to access the ternary's condition text, at least, or even an array of the variables involved in the condition so I have some kind of hope to access it, or is there any other way to do one-line conditions like this, without assigning a new variable?
I'm working with a form that filters data.
If the user does not put in any data the id will return undefined. If not it can be used in the if statement like: if (myValue < userInputValue) which will return true/false. However if nothing is filled in it returns false (of course). Is it possible to change this to true? My alternative solution would be to check for the variables and if it is undefined assign a value myself (however choosing a max number is not very error proof): if (!userInputValue) { userInputValue = 1 }.
What would be the best way to tackle this problem?
if (myValue === undefined || myValue < userInputValue)
or, if you want any falsy value to trigger the condition,
if (!myValue || myValue < userInputValue)
I want to extract a value from a variable. If its an array, I want the first element, and if not, I want the value of that variable. The value is a float, but I was wondering which of these are better in terms of performance, portability to non-floats, and of course short code and code readability
I want to use
value = variable[0] || variable
Its short and nice, is there any caveats is using this?
or I can do,
value = ([].concat(variable))[0]
This SO question says it's bad for performance.
And then, ofcourse, I can check if variable is an array or not in a few different ways, that is also mentioned in above question. Is there any better ways if the first one is not good?
Your value = variable[0] || variable will work, and will work reliably. Technically, if variable is a number primitive, what the JS engine has to do at that point is promote it to an object and then look up the 0 property on that object, but as you know it won't have one, that's okay.
The only cases where that may fail are if variable is null or undefined, because neither of those can be promoted to an object, and so the expression will throw. Provided you're okay with that, then you can use that. I'd comment it, though, because it's pretty odd-looking.
If you needed to defend against null and undefined, you could use the fact that == null will filter out both of those:
value = variable == null ? undefined : variable[0] || variable;
I'd comment that, too. :-)
Having this
value = variable[0] || variable
you may go to a trouble if the first element of the array is false. For example:
var arr = [false, 1, 2, 3];
value = variable[0] || variable; // <--- value is [false, 1, 2, 3]
So, I'll go with this:
var value = arr instanceof Array ? arr[0] : arr;
If you are not sure if the array is full then you should add one more check.
var value = arr instanceof Array && arr.length > 0 ? arr[0] : arr;
Consider the code at the bottom, inside a regular function, that checks if some argument was provided or not, and assigns a default value to a variable named message. If the argument is truthy or an empty string, It is simply converted to a string and is stored in the message variable, otherwise the type of argument will be stored in message.
I know it's possible to shorten if else statements to assign default values to variables, like:
var message = arguments[0] || jQuery.type(arguments[0]);
which if only the arguments[0] is truthy will be stored in message. But how to make an exception for an empty string which is a falsy value, without having to use a long if else statement?
if(arguments[0] || arguments[0] === '')
var message = arguments[0].toString();
else
var message = jQuery.type(arguments[0]);
var message = ((arguments[0] || arguments[0] === '') ? arguments[0].toString() : jQuery.type(arguments[0]));
It sounds like you're looking for a shorthand if/else. If so, you can find the answer to your question here. Basically what you need is a ternary operator.
Excerpt below:
var x = y !== undefined ? y : 1;