Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:12:29+00:00 2026-06-17T18:12:29+00:00

var foo; console.log(typeof foo); //undefined if(typeof foo === ‘undefined’) console.log(1); if(!foo) console.log(2); In my

  • 0
var foo;

console.log(typeof foo); //"undefined"

if(typeof foo === 'undefined')
    console.log(1);

if(!foo)
    console.log(2);

In my example above, the console will log both “1” and “2”, since undefined evaluates as false. The same thing will happen for null, NaN, "" (empty string) etc.

Is it more common to use the typeof operator and compare the string value, rather than using the evaluated boolean value? Is there any difference besides readability? Any pros and cons?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:12:30+00:00Added an answer on June 17, 2026 at 6:12 pm

    There is no silver bullet and it completely depends on your aims.

    If you need to know that variable is “falsy” – you use if (!var), if you need to know precisely if it’s null, 0, empty string or whatever – you use if (var === null)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can we differentiate between var foo; and var foo=undefined; ? typeof foo will
function Foo(){ } Foo.prototype={ method1:function(o){ console.log(o); } ,shorthand:function(){ if(!arguments.length || typeof arguments[0]=='undefined') { arguments[0]={};
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
for (var i = 0; i < somearray.length; i++) { myclass.foo({'arg1':somearray[i][0]}, function() { console.log(somearray[i][0]);
var foo = 'hello'; var myfunc = function() { console.log(foo); var foo = foo
I've seen others using the following pattern. var bar = function foo(){}; console.log(bar); //
If you execute this code: var foo = {bar: 'baz'}; window.console.log(foo); foo.bar = 'bla';
var foo = { p1: function(){ return this.p2; }, p2: function(){ console.log('i am foo.p2');
When I do var foo = $('n'); console.log(foo); I get jQuery object. However if
var foo = 'bar'; console.log(window.foo); // bar Seems like variables get assigned as properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.