In Javascript, why a space bar “converts” to a number in my code?












-1















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!










share|improve this question




















  • 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















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!










share|improve this question




















  • 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








-1








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















-1














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.






share|improve this answer
























  • 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 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 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



















-2














In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0






share|improve this answer





















  • 1





    That's irrelevant. The code in the question never compares a space to zero.

    – Quentin
    Nov 15 '18 at 16:20











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
});


}
});














draft saved

draft discarded


















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









-1














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.






share|improve this answer
























  • 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 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 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
















-1














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.






share|improve this answer
























  • 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 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 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














-1












-1








-1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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 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











  • 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











  • @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

















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













-2














In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0






share|improve this answer





















  • 1





    That's irrelevant. The code in the question never compares a space to zero.

    – Quentin
    Nov 15 '18 at 16:20
















-2














In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0






share|improve this answer





















  • 1





    That's irrelevant. The code in the question never compares a space to zero.

    – Quentin
    Nov 15 '18 at 16:20














-2












-2








-2







In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0






share|improve this answer















In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0







share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values