0

I have a program written in JS. It compares output of an function with number 4. However, it seems it cannot compare properly.

var myStringNotValidated =  "3T-4T";

var notValidate = myStringNotValidated.substring(3, 4);

if(notValidate === 4)
{
console.log("Value1");
}
else
{
console.log("null");
}
//OUTPUT IS NULL, WHICH IDEALLY SHOULD BE "Value1"

Can any one guide me where am I making mistake?

  • 1
    `notValidate` is **String**. `===` compares type too. Convert the string to number or compare it with string `'4'`. – Tushar Jul 06 '18 at 09:11
  • 3
    Possible duplicate of [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) –  Jul 06 '18 at 09:14
  • Or compare with `==` so if one is a Number, both are coerced to Number for the comparison. – RobG Jul 06 '18 at 09:14

6 Answers6

2

You are trapped in the same mistake which most JS developers do!

There is an difference between === and == in JS. (Difference between == and === in JavaScript). Because of this, when you compare "4" with 4, it compares (String 4) with (Numeric 4), hence returning your else condition.

Solution: Just replace === with ==

var myStringNotValidated =  "3T-4T";
var notValidate = myStringNotValidated.substring(3, 4);
if(notValidate == 4)
{
console.log("Value1");
}
else
{
console.log("null");
}

https://jsfiddle.net/pfrvn485/

Code_Tech
  • 775
  • 13
  • 42
1

It depends, if you want to check if notValidate is a string:

var myStringNotValidated =  "3T-4T";

var notValidate = myStringNotValidated.substring(3, 4);

if(notValidate === "4") // Change here
{
console.log("Value1");
}
else
{
console.log("null");
}

if you want to check if notValidate is a number, use parseInt():

var myStringNotValidated =  "3T-4T";

var notValidate = parseInt(myStringNotValidated.substring(3, 4)); // Change here

if(notValidate === 4)
{
console.log("Value1");
}
else
{
console.log("null");
}
Damian Peralta
  • 1,846
  • 7
  • 11
1

Your if condition is wrong if(notValidate === 4) checks 4 as string. either you can use if(notValidate=='4') or convert it to number & check.

1

This is because === compares both value and type. In your program, noValidate contains a value of 4 but its type is string and you are comparing it with 4 whose type is number.

You can either use == or typecast noValidate to number.

var myStringNotValidated =  "3T-4T";

var notValidate = Number(myStringNotValidated.substring(3, 4));
console.log(typeof notValidate);
if(notValidate === 4)
{
console.log("Value1");
}
else
{
console.log("null");
}
Sawan Patodia
  • 773
  • 5
  • 13
1

You're comparing the string("4") with the int type(4). Need to be converted to an integer before comparing.

var myStringNotValidated =  "3T-4T";

var notValidate = parseInt(myStringNotValidated.substring(3, 4));

if(notValidate === 4)
{
console.log("Value1");
}
else
{
console.log("null");
}
1

Try this :

var myStringNotValidated =  "3T-4T";

var notValidate = myStringNotValidated.substring(3, 4);

if(notValidate === "4")
{
console.log("Value1");
}
else
{
console.log("null");
}
Sudipta Basak
  • 179
  • 13