0
 <script type="text/javascript">
//var EProductId = prompt('Please enter your product Id');

var EProductId = [];
EProductId[0] = prompt("New member name?");

//Product price, code, name, declare here

var ProductId = [];
    ProductId[0] = 001;

var product = [];
    product[0] = "tshirt";

var ProdPrice = [];
    ProdPrice[0] = 299;

//Condition start here

if (ProductId[0] === EProductId[0]) {

//  var EProductId2 = parseInt(prompt("Please enter a product", "")ProductId[0] + ' ' + product[0] + ' ' + ProdPrice[0]);
    prompt(ProductId[0] + ' ' + product[0] + ' ' + ProdPrice[0]);
} else{
    alert("You pressed Cancel or no value was entered!");
}
 </script>

Why it is not entering in if condition. i am entering the value 001 after run a program but it alert the message is You pressed Cancel or no value was entered!

Kabir Khan
  • 67
  • 7
  • 1
    Types are different as you're using `===` – Tachyon Mar 02 '15 at 18:19
  • What @Mr.Singh Is saying is that you are using strict comparison: it will check if the values are the same (`'0' == 0` for example) and if they are of the same type ( 2 strings, 2 numbers, but not 1 of each). Example: `0 === 0` returns `true` while `0 === '0'` returns `false`. – Ismael Miguel Mar 02 '15 at 18:53
  • Thanks @IsmaelMiguel for the explanation. I should've elaborated on that. – Tachyon Mar 02 '15 at 18:56
  • @Mr.Singh You're welcome. This actually was my first answer on SO in Portuguese. You can post as an answer and you would have the most upvotes since it would be the most complete answer. – Ismael Miguel Mar 02 '15 at 19:07
  • Thanks @IsmaelMiguel. However, I think people have done a pretty good job here. – Tachyon Mar 02 '15 at 19:12
  • @Mr.Singh Honestly, all the answers are missing this piece of very important information. I'm not pushing you to answer, I'm just saying that you have the oportunity to. – Ismael Miguel Mar 02 '15 at 19:18
  • @IsmaelMiguel Thanks for your suggestion. I've added **your** answer. – Tachyon Mar 02 '15 at 19:25

4 Answers4

1
if (ProductId[0] == EProductId[0]) {}

Only use === when comparing types.

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
1

=== is only used for strict comparisons in javascript. For example: if('0' == 0) would return true as only values are compared here. However, === would check if they are of the same type (2 strings, 2 numbers, but not 1 of each). So, 0===0 returns true while 0==='0' returns false.

You should use == instead. Using == you can compare if values of different types are 'truthy' or 'falsy'. Example: 0 == 0 returns true as well as '0' == 0.

Tachyon
  • 452
  • 7
  • 19
  • 1
    Don't forget to add another example. Saying, for example: "You should use `==` instead. Using `==` you can compare if values of different types are 'truthy' or 'falsy'. Example: `0 == 0` returns `true` as well as `'0' == 0`." – Ismael Miguel Mar 02 '15 at 19:41
  • You're welcome. I would just recommend that you split both statements in 2 different lines for better readability. Other than that, everything is fine. I would upvote again if I could. – Ismael Miguel Mar 02 '15 at 19:45
0

I guess it returns "001" as a string, try this

if (ProductId[0] === +EProductId[0])
Subzero
  • 21
  • 3
0

typeof(prompt()) returns string. You will have to convert it to an integer first using parseInt(string, radix). The 10 specifies base 10, the common numbering system used by people.

if (ProductId[0] === parseInt(EProductId[0],10))