1

While I do know that the following question is stupid simple, it is related to a specific situation that I have been unable to find through Google. The following code is in Javascript.

Suppose there is a variable

x = x + 1;

I can see from a tutorial that this is supposed to work. However, how are we supposed to use this variable in a calculation?

I have tried with the following codes

var name = name + 1;
alert(name);

The above outputs "NaN"; whatever that is...

var name = name + 1;
name = 2;
alert(name);

The above outputs 2 which is simply overriding the original variable.

name = prompt("input any number");
var name = name + 1
alert(name);

The above outputs the input provided + 1 as a string, i.e. 01 where the input is "0" without quotes.

I remember from a ruby lesson that we use .to_i in order to convert a string to an integer. How do we go about doing this in Javascript?

5 Answers 5

3
var name = name + 1;

The above code declares a new variable called name which contains whatever name contained before, plus 1. Since name only just came into existence, it doesn't have a numeric value ("Not A Number", or NaN). Adding 1 to NaN gives NaN.

Sign up to request clarification or add additional context in comments.

Comments

3

+ means different things in different contexts. If the two operands are numbers, then it does addition. If one operand is a String, it does String concatenation, so

var x = "2"; // x is the String "2"
alert(x+2); // "22"

var x = 2; // x is the number 2
alert(x+2); // 4

If you want to convert a String to a number, you can do

if (x) x = parseInt(x, 10);

where the second argument is the radix (i.e. the base of the number system), and you should use it. If someone entered 02 for example, the radix prevents javascript from treating that as an octal (or other) number.

Of course, you always need to make sure your variables are defined before you use them. I bet your NaN result is coming from the variable not being defined.

Comments

2

Your issue is that you never initialize name. For example:

var name = 0;
alert(name); // Name is 0
name = name + 1;
alert(name); // Name is 1

If you don't initialize it, it will give you NaN: Not a Number.

To turn a string into a number, use parseInt or parseFloat:

var name = prompt("input any number"); // I input 3
name = parseFloat(name);
name = name + 1;
alert(name); // Name is 4

Comments

1

Use parseInt to convert a string to a number.

The line x = x + 1 says "take the existing value of x, add one to it, and store the resulting value back in x again".

The line var name = name + 1 is meaningless since name does not have an existing value when the statement is executed. It is the same as saying undefined + 1 which is NaN (Not a Number).

Here are some examples of how the + operator works in JavaScript:

 1  +  2  // number + number is a number -> 3
"1" +  2  // string + anything is a string => "12"
 1  + "2" // anything + string is a string => "12"
"1" + "2" // string + string is a string => "12"

Comments

0

NaN means "not a number". Since name has no value when it is first declared, saying "var name = name + 1" doesn't have a numerical meaning, since name is in the process of being declared when used for the first time.

In the second example, name is determined to be a string. Javascript isn't as sensitive to types as some other languages, so it uses + as a concatenation operator instead of a numerical one, since it makes more sense in context,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.