0
Q:

javascript get type of var

var foo = "Hello";
console.log(typeof foo); // string
5
 What value it has 	 			What the output shows you 
 						when you write |console.log(typeof(variable);
Undefined:						"undefined"
Null:							"object"
Boolean:						"boolean"
Number:							"number"
String:							"string"
Function object:				"function"
E4X XML object:					"xml"
E4X XMLList object:				"xml"
NodeList						"Nodelist [more data]"
HTMLCollection					"HTMLCollection(1) [more data]"
2
function doSomething(x) {
  if(typeof(x) === 'string') {
    alert('x is a string')
  } else if(typeof(x) === 'number') {
    alert('x is a number')
  }
}
1
console.log(typeof 93);
// Output = "number"

console.log(typeof 'Maximum');
// Output = 'string'

console.log(typeof false);
// Output = "boolean"

console.log(typeof anUndeclaredVariable);
// Output = "undefined"
3
var x = 12345; 
console.log(typeof x) // number
x = 'string'; 
console.log(typeof x) // string
x = { key: 'value' };
console.log(typeof x) // object
0

New to Communities?

Join the community