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 8137205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:01:43+00:00 2026-06-06T11:01:43+00:00

This is my first attempt to write shorthand if statements however am befuddled by

  • 0

This is my first attempt to write shorthand if statements however am befuddled by why the expanded versions don’t work quite the way I imagined they would.

Code 1 – Does not work

if(document.getElementById == true) {
        alert("The document object model is supported by: " + navigator.appName);   
    }

Code 2 – Does work

if(document.getElementById != false) {
            alert("The document object model is supported by: " + navigator.appName);   
        }

Code 3 – The shorthand that does work

 if(document.getElementById) {
            alert("The document object model is supported by: " + navigator.appName);   
        }

Why is that if I expand the shorthand in 3 to the first code sample not work and why does it work if I have it equal to != false?

  • 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-06T11:01:49+00:00Added an answer on June 6, 2026 at 11:01 am

    Your first if statement:

    if(document.getElementById == true) {
    

    …doesn’t work because document.getElementById is a function, which is a type of object, and an object is not equal to true.

    Your second if statement:

    if(document.getElementById != false) {
    

    …doesn’t really work – even though you think it does – because (I’m assuming) you’ve only tested it in a browser where document.getElementById is defined in which case, again, .getElementById is a function, a type of object, which not equal to false. But, if .getElementById is not defined then the if test will effectively be testing if undefined != false which is also true. So that test isn’t doing what you think it is.

    Your third if statement:

     if(document.getElementById) {
    

    …does work. The reason it works is because JavaScript has the concept of “truthy” and “falsy” expressions. The number 0, the empty string "", undefined, null, NaN, and of course false are all “falsy” values. Pretty much everything else, including non-zero numbers, non-empty strings, and any objects (including functions) are “truthy”. If the expression in the if statement is “truthy” then the if block will be executed.

    (Finally, there really isn’t any need to test whether document.getElementById exists – I’d be amazed if you can find a browser that runs JS but doesn’t define that method.)

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

Sidebar

Related Questions

Because this is my first attempt at an extension method that seems quite useful
This is my first attempt to write a jQuery plugin, this one is supposed
This is my first attempt at reverse engineering, and really, I don't know how
this if my first attempt at using streaming for WCF, and I am struggling
This is my first attempt to create a GUI in MATLAB. I haven't been
this is my first attempt at a responsive design so excuse me if this
This is pretty strange (admitedly, this is my first attempt with python / sqlite),
I'm trying to create a simple threading procedure (granted this is my first attempt
Preface I've rarely ever been a JS developer and this is my first attempt
I'm writing an application using DDD techniques. This is my first attempt at a

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.