What will be the value of a every thing is allowed? [duplicate] - javascript

This question already has answers here:
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 4 years ago.
I want to run this if statement .
Help me to get the value of a.
var a =undefined ;
if(a=1 && a=2 && a=3)
{
console.log("Hello")}
else
{console.log("Error")
}

The issue is with this line if(a=1 && a=2 && a=3).Here the value is getting set to a instead of checking the condition
var a = undefined;
if (a === 1 && a === 2 && a === 3) {
console.log("Hello")
} else {
console.log("Error")
}
i want to run this if statement
a cannot be 1,2,3 at same time unless you are looking for something like this.Otherwise you can use or operator || instead of &&

Related

What does "value != value" mean in JS? [duplicate]

This question already has answers here:
How do you test for NaN in JavaScript?
(3 answers)
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 3 months ago.
I was reading the source code of core-js and I saw the following:
if (value != value) return true;
What does it actually mean? When exactly value won't be equal to itself?
It could be if:
value is NaN
console.log(NaN != NaN);
value is actually a getter on the global object, and it returns something different each time:
let i = 0;
Object.defineProperty(window, 'value', { get: () => i++ });
console.log(value != value);

How can I input a conditional statement in ternary operator to return true or false in React? [duplicate]

This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 6 months ago.
I have the values ​​lightsstasut1error, lightsstasut2error, lightsstasut3error and the strips contain either "Y" or "N". At this time, I want to return true if at least one of the values ​​is N, and false if all of them are Y. So I wrote the code, and if there is even one N, it keeps returning false. How do I fix my code?
this is my code
const errors2 = (lightstasut1error || lightstasut2error || lightstasut3error) === "N" ? true : false;
CHange this to this
const errors2 = (lightstasut1error === "N" || lightstasut2error === "N" || lightstasut3error === "N")
Please check now
Hope it helps, feel free for doubts

if/else statement with prompt and || [duplicate]

This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 2 years ago.
I have a condition:
if (item == 'a' || item == 'b' || item == 'c' || item == 'd' || item == 'e') {
// statements
}
How can I reduce the branching? Is there any other way to write this in JavaScript.
You can also use the newer Array.includes
if (['a','b','c','d','e'].includes(item)) {
...
}
Another option (for the very specific case you posted) would be to compare unicode point values using </>
if (item >= 'a' && item <= 'e') {
...
}
Use Array#indexOf method with an array.
if(['a','b','c','d','e'].indexOf(item) > -1){
//.........statements......
}
You can use an array as shown below.
var arr = ['a','b','c','d','e'];
if(arr.indexOf(item) > -1)
{
//statements
}
This would work nicely:
if('abcde'.indexOf(item) > -1) {
...
}
You could also use the newer String.prototype.includes(), supported in ES6.
if('abcde'.includes(item)) {
...
}

Checking if object not null before accessing property [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
var how = $('how');
var id = $('myId');
if ((how != null && !how.value.blank()) || myId != null) {
return true;
} else {
alert('Error');
}
Is there an easier way to check for not null and checking if the value of that element is blank without having to do both != null and then calling value?
Since null is falsy, a slightly shorter version would be
if((how && !how.value.blank()) || myId != null) {
...
}
Note that the above code and your own snippet both assume that if how exists, it will have a property called value, and will throw an exception if this is not the case.

In an if statement in javascript what is the difference between == and ===? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
In some JavaScript if statements I've seen the use of === as opposed to the standard ==.
What is the difference between these? and should I be using either one of them over the other?
e.g.
if (variable == 'string') {
return;
}
compared to:
if (variable === 'string') {
return;
}
=== also checks to be equal by type
For instance 1=="1" is true but 1==="1" is false
The === is a strict comparison (also checks type), whereas the == does a more relaxed comparison.
For instance:
var a = 'test';
if (a == true) {
// this will be true
}
if ( a === true) {
// this will not be true
}
Another example:
var b = '0';
if ( b == 0){
// this will be true
}
if ( b === 0 ){
// this will not be true
}
In particular it is very important when comparing falsy values. In Javascript all the following will be treated as false with relaxed comparison:
* false
* null
* undefined
* empty string ''
* number 0
* NaN

Categories