How to make 0 not equal to empty string in JavaScript [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 5 years ago.
I know the rules that 0 is equal to empty string '' in javascript. So how to make if statement to differ this? I mean, I have field where I insert amount value. For empty string I need one message to show but for 0 value another. Currently, I always catch 0 == ''.
if (amount == '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}
else if (amount < 5) {
$("#validation-msg1").addClass("opux-is-visible");
return false;
}
EDIT. when I use === I do not get condition equals true when field is empty. What does it mean? My amount value is different type than empty string? I parse it like this:
var amount = parseInput($("#amount").val());

Just use strict compare === that will check value AND type of operands.

Use three equation signs. It checks the type too and 0 will be different from ''.
if (amount === '') {
$("#validation-msg3").addClass("opux-is-visible");
return false;
}

Related

How to use If Else for check validation is number in Javascript [duplicate]

This question already has answers here:
How can I check if a string is a valid number?
(50 answers)
Closed 3 years ago.
This is my code
else if(form.txtPrice.IsNumber){
alert("Price must be number") form.txtPrice.focus();
return false;
I can not check validation must be number in Javascript, everybody help me please, thank you so much!
Use isNaN(value).
This returns true if the value is not a number.
If the value is a number, will return false.
Using only isNaN is risky, it has some big inconvenient; These strings will be considered valid numbers:
'Infinity'
'1e355'
'0xf'
empty string
you could do more:
function isNumber(input) {
return typeof input === "number" && !isNaN(input);
}
note that you have to parse if before if you have a string (parseInt/parseFloat)

Javascript: Why won't my values equal up? [duplicate]

This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Closed 5 years ago.
var check = true;
var number = Math.floor(Math.random() * 20);
while (check === true){
var guess = prompt("I picked a number 0 to 20, try to guess it!");
if (number === guess) {
print("You guessed correctly! Good job!");
check = false;
}
else if (number < guess) {
print("\n\You guessed too high!");
}
else if (number > guess) {
print("\n\You guessed too low!");
}
else {
print("\n\Error. You did not type a valid number");
exit();
}
print("\n\Guess: " + guess + ".");
}
When I try running this program, I get all the way up to the correct answer, but it doesn't work! Even if the randomly generated number is 13, and I guessed 13, it would go through and it would say it is invalid.
Your guess is a string. It is the text entered by the user you need to convert it into a number in order to be able compare it with your guess so replace
var guess = prompt("I picked a number 0 to 20, try to guess it!");
with
var guess = Number(prompt("I picked a number 0 to 20, try to guess it!");
This will turn your guess from your user into a number or a special value NaN if it isn't formatted correctly.
You could also use the == operator which will automatically convert between types. I would recommend against using the operator if you are new to javascript as it can have some confusing and unexpected behaviors.
You are comparing the return value of prompt (a string) with the return value of Math.floor (a number).
Since you are using === and they are different data types, you'll never get a match.
Use == or explicitly cast the number to a string or vice versa.
number === +guess
=== is strictly typed so it will not compare the int to a string.
Convert guess to an integer. You should first validate it though in case the user inputs something other than an int.
var guessInt = +guess; // the plus converts to an integer
if(isNaN(guessInt))
continue;
if (number === guessInt) {
print("You guessed correctly! Good job!");
check = false;
}
You are using the triple = operator which checks for type equalness too. when you compare the prompt value (your guess variable) to your number variable. you are comparing a String and a Number. To make this work you could use
number == guess
or
Number(guess)
You’re using strict equality comparison, which also compares types.
prompt returns string values. Use parseInt to cast to a number.
var guess = prompt("I picked a number 0 to 20, try to guess it!");
guess = parseInt(guess, 10);
The second parameter tells the number base (10 is for decimal, 16 for hexadecimal, etc.). In non strict mode (aka sloppy mode) you may experience accidental conversion to octal (base 8) when parsing strings with a leading zero. Always specify the base to avoid this.
You might want to learn more about JavaScript strict mode.

In JavaScript what's the difference between Equal to ('==' & '===') [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I am trying to understand equality in JavaScript. Here is the code.
var x = prompt("What is 10 + 10");
if (x === 10) {
document.write("Correct")
}
else {
document.write("Incorrect")
}
Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?
I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!
=== is strict type equality which compares by both value and type
== is non-strict type equality, which compares only by value.
In other words, == performs type conversion and then compares values for equality. Here are some examples
"3" == 3
=> true
Explanation: The string 3 is converted to the number 3, which is equal to 3.
"3" === 3
=> false
Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.
In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.
In your case, the interpreter sees it like this
if ("10" === 10) {
// does the string "10" equal the number 10? If so
document.write("Correct")
}
else {
// Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
document.write("Incorrect")
}
In Javascript,
== means: is equivalent to
=== means: is identical to
When the value of x is "10", x is equivalent to 10.
But it isn't identical to 10.

How "==" works in JavaScript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
I found == is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
== is called comparison/equality operator, it compares 2 values, but not their data types so for example
1 == '1' will return true, for stricter comparison, use === which will compare the data types too so 1 === '1' will return false
== is a comparison operator that means "equal to" but does not take variable typing into account.
=== is a stricter comparison operator that means "equal to and same type".
So if you have a string called numberStr with a value of 2 and an integer called numberInt with a value of 2, they will evaluate as follows:
numberStr == numberInt // evaluates to true
numberStr === numberInt // evaluates to false because types are different

Values comparison [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
[4] === 4 // is: false
[4] == 4 // is: true
'0' == 0 // is: true
'0' === 0 // is: false
Can anyone give the exact reason for this?Also what exactly does strict equality operator do or need for comparision?I learned that type and value should be same for strict(===) operator.Is this what strict equality operator checks .If yes,than how equl to operator works?
== Compare values
=== Compare values and type
For example
[4] //turns into "4" when comparing
"4" == 4 //They are the same
"4" === 4 //The values are the same, but not the type
Reference: http://es5.github.io/#x11.9.4
http://i.stack.imgur.com/q13LO.png
The === operator also compares the type of the object.
So, in [4] === 4
[4] is an array, but 4 is a number, so that evaluates to false.
And in '0' === 0
'0' is a string, but 0 is a number, so that evaluates to false.
The === operator compares both type and value, making it a stricter check.
The == operator performs a less strict value based checked. It will in some cases consider values of different types "equal" Such examples are 0 vs '' or 0 vs "0"
The == operator will see 0 and '' as equal whereas the === operator will not treat them as equal since they are of different types.

Categories