This JavaScript article tutorial guides you how to check type of JavaScript variables. This JavaScript tutorial guide contains full-completed JavaScript example codes and comments, it's very easy to follow, please go to the full-post page for details.
Other JavaScript tutorials for beginners:
- JavaScript OOP Tutorial for Dummies
- 10 Tiny JavaScript Snippets for Good Beginners
- Top 10 Best JavaScript eBooks that Beginners should Learn
- The basics of Javascript for beginners
- Demo
- Enlarge
- Reload
- New window
Generate your business videos by AI with voice or just text
Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
foo versus window.foo and this.foo
The secret to our solution lies in how JavaScript handles undefined variables versus undefined properties. Here�s a quick reminder on the difference between the two:
// variable foo var foo; // property bar (and object variable foo) var foo = {}; // create an empty object to add bar too foo.bar = "";
The difference between these makes all the difference whether a fatal error occurs:
foo; // ReferenceError: foo is not defined var foo = {}; foo.bar; // undefined foo.blah; // undefined
We can randomly invent and check any property of foo
we want, and the code will keep chugging along:
var foo = {}; foo.something = "hello"; if(foo.bar) { // undefined, interpreted as false (same as !!foo.bar) // never runs } if(foo.something) { foo.something; // "hello" }
Now comes the magic. If you know anything about JavaScript running
in the browser, you know that all global variables are part of the window
object. This means foo
and window.foo
are equivalent:
var foo; foo === window.foo; // true // "this" is another bag of worms, but note this anyway this.foo === window.foo;
So technically our variable foo is a property of the window object. So we should be able to check for any arbitrary variable in the window scope now!
window.foo; // undefined (not a fatal error!) // even though we're checking for the same thing, we get a fatal error� foo; // ReferenceError: foo is not defined
Practical uses
Now as long as we have a global entry point for our code, we can write our code in such a way that it won�t ever give us a fatal error if our variables aren�t yet defined:
if(window.foo && foo.bar) { foo.bar(); }
Of course nothing happens, because we haven�t defined foo. But why
doesn�t foo.bar give us a fatal error? Because the first test, window.foo
failed out. It would be useless processing for the JavaScript engine
to also evaluate the second statement, because the end result will still
be the same (false && true results in false, false &&
false results in false). So it doesn�t get so far as foo.bar
.
And now the code will work properly when we hook up our code to our foo
global namespace:
var foo = { bar: function() { alert("hello world"); } } if(window.foo && foo.bar) { foo.bar(); // "hello world" }
Shorthand
It�s becoming common to see an abbreviation for the above code. Check out the following two methods, which accomplish the same thing:
// Method 1 if(window.foo && foo.bar) { foo.bar(); // "hello world" } // Method 2 window.foo && foo.bar && (foo.bar());
Don�t get too scared.. these blocks of code are equivalent. Method 2
is shorter, but it simply goes something like this: if window.foo isn�t
set, stop evaluating this line (just as above in the if
statement). Otherwise, if foo.bar exists then execute the code in parentheses, which gives us an alert (�hello world�);
Just a word of caution about using Method 2: it�s obviously less clear what�s going on here. Your own cleverness might actually result in unnecessary confusion, especially of other people work in the same code base and they�re at different levels of understanding. In these cases we should first strive to be clear, and then only clever if it�s not at the expense of being clear.
It�s also likely that a code minifier would take care of this cleverness for us at build time, giving us the best of both worlds.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AI Voice Generator
Most Realistic Text to Speech AI Solutions
AI Voice Generator
Most Realistic Text to Speech AI Solutions
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k