I would like to compare two strings and decide if they are not equal. I have the code below, but it seems that it is not returning what I want, is this correct?
if ((current.request.requested_for != reviewer) && (current.request.requested_for != approver)) {
return 'Yes';
}
return 'No';
}
Here reviewer and approver, are some strings that I have declared in lines prior to the if conditions. Basically, my question here is to know if I should use != to check if 2 strings are not equal.
Thanks
Assuming that you have strings in the variables reviewer, approver and the object current.request.requested_for, you can just compare two strings with === which compares type and value. If you compare with ==, you are just comparing the value, there are more reasons about == is returning true for '2' == 2, visit this.
So, your code could be more declarative and simpler
(...)
const SEARCH = [reviewer, approver];
return SEARCH.includes(current.request.requested_for)
(...)
If your are not using latest javascript do not worry, you could achieve the same with an array declaration and the indexOf() method. They are the former way of the code.
Related
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I'm learning JavaScript and I found weird(?) behavior of JavaScript.
I create date objects by
var stack = new Date(1404187200000) // 07-01-2014
var overflow = new Date('07-01-2014')
And when I compare those two date objects
stack == overflow // returns false
stack.getTime() == overflow.getTime() // returns true
And I believe it's because they are not the same object. But I know that '==' is comparison of equality and '===' is comparison of identity - like this example:
var stack = 1;
var overflow = '1';
stack == overflow // returns true
stack === overflow // returns false
So, why does comparing new Date([NUMBER]) and new Date([STRING]) give a different result even though they are the same date?
Please enlighten me!
You're misunderstanding the difference between == and ===. It's not that one does equality checking and one does reference checking.
For ===, the two operands have to have the same type. But for ==, type coercion is allowed before checking for equality.
In your case, the two objects are of the same type, so there's no difference between == and ===; but they are checking reference equality, not value. The right way to check for value equality with dates is as you're doing: check whether stack.getTime() == overflow.getTime().
You can also do +stack == +overflow, which will cast them both first, and then you'll get a value equality test.
new Date returns an object. Each time you create it it will create a different object, so they're not equal. getTime returns a value (property) from the object-- this will be the same for both objects.
This is a little bit more complicated. === checks the type while == tries to convert to a common type. That's why 1 == '1' is true but 1 === '1' is false, because in the first case '1' gets transformed to a number (AFAIR).
you can see the exact specification how that is handled here: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 - the interesting part for you in this case is 1. f.
Return true if x and y refer to the same object. Otherwise, return false.
i've got the following situation.
In my script I have extracted all strings, that are publicated to the webpage, into an object like this.
var strings = {
one: "One",
two: "Two",
three: "Three",
}
Please don't ask why I want to do this, it's just a test case.
Now I want to push one of these strings, for example, into an alert(). this would look like this:
alert(strings.one);
So far so good, but I want to check if strings or strings.one exist and when it doesn't return an empty string.
What is a slick way to do this, without using the classic if(strings.one == undefined)?
EDIT
I've found a solution according to your answers and comments.
alert((window.strings) ? strings.one || "nope" : "nope");
This catches all cases i want to prevent:
I forgot to declare strings
strings.one doesn't exists
I hope this fits to "slick way"?!
You can use ||, which returns the first operand if it's truthy and the second otherwise:
alert(strings.one || "");
This will also catch other falsy values, but that probably won't be an issue for you.
You can use the ternary operator or u can do this if you want and empty string if string.one does not exist.
alert(string.one || '');
How to check if javascript variable exist, empty, array, (array but empty), undefined, object and so on
As mentioned in the title, I need a general overview how to check javascript variables in several cases without returning any error causing the browser to stop processing the pageload.
(now I have several issues in this topic.
For example IE stops with error in case _os is undefined, other browsers doesnt:
var _os = fbuser.orders;
var o =0;
var _ret = false;
for (var i = 0; i < _os.length; i++){
...
Furthermore i also need a guide of the proper using operators like == , ===.
As mentioned in the title, I need a general overview how to check
javascript variables in several cases without returning any error
causing the browser to stop processing the pageload.
To check whether or not variables is there, you can simply use typeof:
if (typeof _os != 'undefined'){
// your code
}
The typeof will also help you avoid var undefined error when checking that way.
Furthermore i also need a guide of the proper using operators like ==
, ===.
Both are equality operators. First one does loose comparison to check for values while latter not only checks value but also type of operands being compared.
Here is an example:
4 == "4" // true
4 === "4" // false because types are different eg number and string
With == javascript does type cohersion automatically. When you are sure about type and value of both operands, always use strict equality operator eg ===.
Generally, using typeof is problematic, you should ONLY use it to check whether or not a variables is present.
Read More at MDN:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof
The typeof operator is very helpful here:
typeof asdf
// "undefined"
Perhaps you want something like this in your case:
// Handle cases where `fbuser.orders` is not an array:
var _os = fbuser.orders || []
This is from Crockford's JavaScript: The Good Parts
var is_array = function (value) {
return Object.prototype.toString.apply(value) === '[object Array]';
};
Would this code have worked just as well if he had used a simple equality compare == instead of the identity compare ===?
My understanding of identity is that it allows you to check if a value is really set to something specific, and not just something equivalent. For example:
x == true
Will evaluate to true if x is 1, or true, but
x === true will only be true if x is true.
Is it ever possible for the is_array function above to work with either == or ===, but not the other?
In this particular case == and === will work identically.
There would be no real difference in this case because both sides of the quality test are already strings so the extra type conversion that == could do won't come into play here. Since there's never any type conversion here, then == and === will generate the same result.
In my own personal opinion, I tend to use === unless I explicitly want to allow type conversion as I think there is less likelihood of getting surprised by some result.
You are correct. With == instead of === it should work fine.
=== is a strict match, and will not return true for 'falsy' or 'truthy' values (see this for more details). Which shouldn't apply in this situation.