-2

Hi I want to check primitive types using javascripfor example

var x //some kind of input is needed like prompt?
if x is a string then alert typeof x is a string

else if x is a boolean alert typeof  x  is a boolean

else 
return alert x is a number 

I dont know how to use typeof to check this because i just started learning javascript. Thank you!

  • 1
    If you don't know how an operator works, **read the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)**. Then ask for clarification if you didn't understand everything. – Felix Kling Jul 06 '14 at 00:33
  • Given that you've explicitly used the string `typeof` of in your pseudo code, where are you stuck? Hint: `typeof` would be useful for the first part. Incidentally: *any* input taken from a user via the `prompt()` is going to be a string. – David Thomas Jul 06 '14 at 00:34
  • Possible duplicate of [Finding Variable Type in JavaScript](http://stackoverflow.com/q/4456336/218196) – Felix Kling Jul 06 '14 at 00:41

1 Answers1

0
typeof "John"                 // Returns string 
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object
typeof {name:'John', age:34}  // Returns object

Here's some documentation

Typo
  • 1,875
  • 1
  • 19
  • 32
  • 1
    FWIW, Array is not a data type in JS (the article make it looks like it is). – Felix Kling Jul 06 '14 at 00:38
  • @FelixKling I'm sorry I don't know what FWIW means but thank you anyways. Although it seems to return object, doesn't it? – Typo Jul 06 '14 at 00:49
  • FWIW means "for what it's worth". Yes it returns "object" which is correct, but the article is titled "data types" and lists Array, even though it's not a data type. I just wanted to point out this minor discrepancy. – Felix Kling Jul 06 '14 at 01:03
  • @FelixKling OIC...hehe – Typo Jul 06 '14 at 01:04