In Javascript, why a space bar “converts” to a number in my code?
Good afternoon!
I have found this code and I am surprised that "backspace"
on a keypad converts it to a NUMBER
:
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
Why?? And - how to treat it?
And, please, my second question:
there was written let x = null
. May I write let x = ""
instead? Or only let x
?
Many thanks in advance!
javascript
|
show 3 more comments
Good afternoon!
I have found this code and I am surprised that "backspace"
on a keypad converts it to a NUMBER
:
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
Why?? And - how to treat it?
And, please, my second question:
there was written let x = null
. May I write let x = ""
instead? Or only let x
?
Many thanks in advance!
javascript
1
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
What is this supposed to be doing?"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
2
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06
|
show 3 more comments
Good afternoon!
I have found this code and I am surprised that "backspace"
on a keypad converts it to a NUMBER
:
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
Why?? And - how to treat it?
And, please, my second question:
there was written let x = null
. May I write let x = ""
instead? Or only let x
?
Many thanks in advance!
javascript
Good afternoon!
I have found this code and I am surprised that "backspace"
on a keypad converts it to a NUMBER
:
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
Why?? And - how to treat it?
And, please, my second question:
there was written let x = null
. May I write let x = ""
instead? Or only let x
?
Many thanks in advance!
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
let x = null;
x = prompt("Write something: ", "");
if((x == null) || (x == "")) {
alert("Nothing was written");
} else {
if("NaN" == 1*x+"") {
alert("A string was written");
} else {
alert("A number was written");
}
}
javascript
javascript
edited Nov 15 '18 at 14:00
Nikos M.
4,54521825
4,54521825
asked Nov 15 '18 at 13:52
JoanJoan
32
32
1
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
What is this supposed to be doing?"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
2
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06
|
show 3 more comments
1
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
What is this supposed to be doing?"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
2
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06
1
1
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
What is this supposed to be doing?
"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
What is this supposed to be doing?
"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
2
2
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06
|
show 3 more comments
2 Answers
2
active
oldest
votes
let x;
x = prompt("Write something: ", "");
console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
//if x is a letter prints 'NaN'
//if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
//if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
//if x is a space, the result is 0 | 1 * " " = 0
//if x is null, the result is 0 | 1 * null = 0
//if x is a letter, returns NaN | 1 * A = NaN
//if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
//if the current value of x is 0 we are left with "NaN" == 0, which is false
//if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
//if the current value of x is a number, we are left with "NaN" == *a number* which is false
if ((x === null) || ( x.trim() === "")) { //check if x is empty space
alert("Nothing was written");
} else {
if (Number(x) === "NaN") { //will return true if x contains letters
alert(Number(x));
} else {
alert(Number(x)); //we end up here if x is a number
}
}
Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace theNumber(x)
afterNumber(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.
– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changedalert("A number " +x+ " entered");
in the lastelse
and since I have the phraseA number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts
– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my(x=="")
with your(x.trim() === "")
and the code works excellently. Bye!
– Joan
Nov 19 '18 at 9:03
add a comment |
In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320998%2fin-javascript-why-a-space-bar-converts-to-a-number-in-my-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
let x;
x = prompt("Write something: ", "");
console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
//if x is a letter prints 'NaN'
//if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
//if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
//if x is a space, the result is 0 | 1 * " " = 0
//if x is null, the result is 0 | 1 * null = 0
//if x is a letter, returns NaN | 1 * A = NaN
//if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
//if the current value of x is 0 we are left with "NaN" == 0, which is false
//if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
//if the current value of x is a number, we are left with "NaN" == *a number* which is false
if ((x === null) || ( x.trim() === "")) { //check if x is empty space
alert("Nothing was written");
} else {
if (Number(x) === "NaN") { //will return true if x contains letters
alert(Number(x));
} else {
alert(Number(x)); //we end up here if x is a number
}
}
Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace theNumber(x)
afterNumber(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.
– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changedalert("A number " +x+ " entered");
in the lastelse
and since I have the phraseA number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts
– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my(x=="")
with your(x.trim() === "")
and the code works excellently. Bye!
– Joan
Nov 19 '18 at 9:03
add a comment |
let x;
x = prompt("Write something: ", "");
console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
//if x is a letter prints 'NaN'
//if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
//if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
//if x is a space, the result is 0 | 1 * " " = 0
//if x is null, the result is 0 | 1 * null = 0
//if x is a letter, returns NaN | 1 * A = NaN
//if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
//if the current value of x is 0 we are left with "NaN" == 0, which is false
//if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
//if the current value of x is a number, we are left with "NaN" == *a number* which is false
if ((x === null) || ( x.trim() === "")) { //check if x is empty space
alert("Nothing was written");
} else {
if (Number(x) === "NaN") { //will return true if x contains letters
alert(Number(x));
} else {
alert(Number(x)); //we end up here if x is a number
}
}
Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace theNumber(x)
afterNumber(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.
– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changedalert("A number " +x+ " entered");
in the lastelse
and since I have the phraseA number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts
– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my(x=="")
with your(x.trim() === "")
and the code works excellently. Bye!
– Joan
Nov 19 '18 at 9:03
add a comment |
let x;
x = prompt("Write something: ", "");
console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
//if x is a letter prints 'NaN'
//if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
//if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
//if x is a space, the result is 0 | 1 * " " = 0
//if x is null, the result is 0 | 1 * null = 0
//if x is a letter, returns NaN | 1 * A = NaN
//if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
//if the current value of x is 0 we are left with "NaN" == 0, which is false
//if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
//if the current value of x is a number, we are left with "NaN" == *a number* which is false
if ((x === null) || ( x.trim() === "")) { //check if x is empty space
alert("Nothing was written");
} else {
if (Number(x) === "NaN") { //will return true if x contains letters
alert(Number(x));
} else {
alert(Number(x)); //we end up here if x is a number
}
}
Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.
let x;
x = prompt("Write something: ", "");
console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
//if x is a letter prints 'NaN'
//if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
//if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
//if x is a space, the result is 0 | 1 * " " = 0
//if x is null, the result is 0 | 1 * null = 0
//if x is a letter, returns NaN | 1 * A = NaN
//if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
//if the current value of x is 0 we are left with "NaN" == 0, which is false
//if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
//if the current value of x is a number, we are left with "NaN" == *a number* which is false
if ((x === null) || ( x.trim() === "")) { //check if x is empty space
alert("Nothing was written");
} else {
if (Number(x) === "NaN") { //will return true if x contains letters
alert(Number(x));
} else {
alert(Number(x)); //we end up here if x is a number
}
}
Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.
answered Nov 15 '18 at 14:40
Katie.SunKatie.Sun
587114
587114
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace theNumber(x)
afterNumber(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.
– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changedalert("A number " +x+ " entered");
in the lastelse
and since I have the phraseA number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts
– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my(x=="")
with your(x.trim() === "")
and the code works excellently. Bye!
– Joan
Nov 19 '18 at 9:03
add a comment |
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace theNumber(x)
afterNumber(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.
– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changedalert("A number " +x+ " entered");
in the lastelse
and since I have the phraseA number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts
– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my(x=="")
with your(x.trim() === "")
and the code works excellently. Bye!
– Joan
Nov 19 '18 at 9:03
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Who is down voting all of the answers on this page?
– Katie.Sun
Nov 15 '18 at 15:02
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-)
– Joan
Nov 16 '18 at 9:14
@Joan to change the alert text all you would have to do is replace the
Number(x)
after Number(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.– Katie.Sun
Nov 16 '18 at 13:40
@Joan to change the alert text all you would have to do is replace the
Number(x)
after Number(x) === "NaN"
with the text you would want to appear. and yes, in English it's just space.– Katie.Sun
Nov 16 '18 at 13:40
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changed
alert("A number " +x+ " entered");
in the last else
and since I have the phrase A number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts– Joan
Nov 19 '18 at 8:43
@Katie.Sun Thanks! Unfortunately the code works curiously: I have only changed
alert("A number " +x+ " entered");
in the last else
and since I have the phrase A number...entered
in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts– Joan
Nov 19 '18 at 8:43
@Katie.Sun Hurrah, I have solved :-) I have only replaced my
(x=="")
with your (x.trim() === "")
and the code works excellently. Bye!– Joan
Nov 19 '18 at 9:03
@Katie.Sun Hurrah, I have solved :-) I have only replaced my
(x=="")
with your (x.trim() === "")
and the code works excellently. Bye!– Joan
Nov 19 '18 at 9:03
add a comment |
In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
add a comment |
In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
add a comment |
In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0
In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0
edited Nov 20 '18 at 10:12
answered Nov 15 '18 at 14:12
AleksWAleksW
1239
1239
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
add a comment |
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
1
1
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
That's irrelevant. The code in the question never compares a space to zero.
– Quentin
Nov 15 '18 at 16:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320998%2fin-javascript-why-a-space-bar-converts-to-a-number-in-my-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
stackoverflow.com/questions/9307603/…
– epascarello
Nov 15 '18 at 13:58
What is this supposed to be doing?
"NaN" == 1 * x + ""
– Katie.Sun
Nov 15 '18 at 13:58
"And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates.
– Quentin
Nov 15 '18 at 13:58
You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter.
– epascarello
Nov 15 '18 at 13:58
2
Have you tried debugging your code and see value of x after prompt?
– barbsan
Nov 15 '18 at 14:06