2

For a variable x=5, how do I know it is number five or the character '5'?

Btw, in JS, do characters follow the ASCII table? Then can I manipulate a character variable. For example, if variable x is character a, can I do x=x+1 to make it character b?

4
  • Don't ask two different questions in the same question. And doing at least a cursory glance at google might help you answer your own questions. Commented Jun 6, 2012 at 2:58
  • Regarding the second part of your question, why don't you just try and see what happens, if you already know what to expect? Commented Jun 6, 2012 at 2:59
  • I thought questions like this would get mostly "google it" and massive downvotes, I'm surprised to see a flurry of answers below though... Commented Jun 6, 2012 at 3:00
  • @SiGanteng: sometimes it takes about the same time to type lmgtfy than to type an actually helpful answer. Plus we do tend to make the same mistake over and over of trying to help people who can't help themselves. We're naturally trustring and naive. :) Commented Jun 6, 2012 at 3:01

5 Answers 5

2

To see if x is the number 5, as opposed to the string "5", you can use the identity operator:

if (x === 5) {
}

Identity will not do any implicit conversions; it will return true only if both operands are equal without any conversions.


For example, if variable x is character a, can I do x=x+1 to make it character b?

No. x = x + 1 will convert 1 to a string, perform string concatenation and return "a1"

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

2 Comments

triple equal signs? is it supposed to be double?
triple checks for type and value, it's a stricter checking than using two =
2

You can use

typeof x;

which returns a string describing the type of the variable, like number, string or object.

To get the character code of a character, use charCodeAt:

var mystring = 'a';
mystring.charCodeAt(0);

And to get a character from an augmented char code, use String.fromCharCode :

var nextLetter = String.fromCharCode( mystring.charCodeAt(0) + 1 ); // returns "b"

This creates a new string from the incremented char code from the first character in mystring.

Comments

1

Just get the type of the variable:

console.log(typeof '5'); // Returns 'string';
console.log(typeof 5);   // Returns 'number';

As for your second question, no, it doesn't work:

console.log('b' + 1);    // Returns 'b1'

4 Comments

How about "var x=5"? Is is a number or a char?
@user1229490: I think you're a bit confused. var x = 5; console.log(typeof x); // outputs 'number'
You defined it as a number, so therefore it is a number?
@user1229490 Javascript doesn't have a char type. It's either a String or a Number.
1

To check if a variable is a number:

if (typeof x == 'number')
    // x is a number

Doing this x = x + 1 when x = b, would result in the string 'a1';

1 Comment

Strictly, variables don't have a type, they have a value. The value of the variable has a Type.
0

If your var x is not an integer, this is the best way to change it to integer..

To change the x to integer

x = parseInt(x);

You can how add any value to the x, example:

x = x + 2;

Since we have changed the x to integer, we can now identify if this is equal to 5

if(x == 5){
 //Your Codes Here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.