Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to use typeof operator to get the the type of a variable, object or function in JavaScript

3 Answers

0 votes
document.write(typeof "abc" + "<br />"); // string
document.write(typeof 10 + "<br />"); // number
document.write(typeof 3.14 + "<br />"); // number
document.write(typeof false + "<br />"); // boolean
document.write(typeof [11, 34, 2, 99, 100] + "<br />"); // object
document.write(typeof function () {} + "<br />"); // function
document.write(typeof null + "<br />"); // object
document.write(typeof {name:'mr bin', salary:89842}  + "<br />"); // object
document.write(typeof NaN + "<br />"); // number
document.write(typeof new Date() + "<br />"); // object
document.write(typeof n + "<br />"); // undefined
document.write(typeof 10/2 + "<br />"); // NaN
 
  
/*
run:
  
string
number
number
boolean
object
function
object
object
number
object
undefined
NaN
  
*/

 



answered Apr 17, 2017 by avibootz
edited Nov 8, 2019 by avibootz
0 votes
function f(a) {
    return a * a * a;
}

var n = 37;
var s = "abc";
var arr = [1, 2, 3];

class worker {
  constructor() {
    this.id = 999928;
    this.name = 'R2';
  }
}
const w1 = new worker();

var b = true;

const user = {
  id: 83829,
  name: 'Tom'
};

console.log(typeof n); // number
console.log(typeof s); // string 
console.log(typeof arr); // object
console.log(typeof worker); // function
console.log(typeof w1); // object
console.log(typeof b); //boolean
console.log(typeof f); // function
console.log(typeof (() => {})); // function
console.log(typeof user); // object



/*
run:
          
number
string 
object 
function 
object 
boolean 
function 
function 
object
    
*/

 



answered Nov 8, 2019 by avibootz
0 votes
var a;
let b;
const c = 9;


console.log(typeof a); // undefined
console.log(typeof b); // undefined
console.log(typeof c); // number
 
  
/*
run:
  
undefined
undefined
number

*/

 



answered Nov 8, 2019 by avibootz

Related questions

...