How to check if a JavaScript variable is NOT undefined?

JavascriptUndefined

Javascript Problem Overview


Things I’ve tried that don’t seem to work:

if(lastName != "undefined")

if(lastName != undefined)

if(undefined != lastName)

Javascript Solutions


Solution 1 - Javascript

var lastname = "Hi";

if(typeof lastname !== "undefined")
{
  alert("Hi. Variable is defined.");
} 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChuck Le ButtView Question on Stackoverflow
Solution 1 - Javascriptsbeliv01View Answer on Stackoverflow