DONATE  
MENU
AJAX
JSON
HTML
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Undefined
Share Tweet WhatsApp

JavaScript Undefined

Undefined is a data type assigned to variables without values. It is also assigned when a variable is use but was neither declared nor assigned a value.
var x;
alert(x);// will alert "undefined"
When you create and use a variable without assigning a value, it automatically holds a type and a value - undefined. In most programming languages such as C, Java and PHP, using a variable without a value is prohibited and leads to error but JavaScript allows such.
<script>
var country;

alert("He is from "+country);
</script>

Copy PRACTICE IT

Same scenario applies to functions and objects' methods' parameters whose value were not assigned during invocation. They are automatically assigned a data type and a value of undefined.
<script>
function myFunc(x){
alert(x);
}

myFunc();//No parameter
</script>

Copy PRACTICE IT

Therefore, it is always necessary to assign values to variables. Undefined typeof() The typeof() operator will return undefined when use on a variable without value.
<script>
var x;

alert(typeof(x));
</script>

Copy PRACTICE IT

Default Assignment It is a good practice to always assign values to variables during declaration even when those values are not the supposed values for those variables. Below is how you can assign default values to variables,
var num = 0; //For numbers
var str = ""; /*OR */ var str = ''; //For strings
var bool = false; //For booleans
var array = []; //For arrays
var obj = {}; //For objects
var myFunc = function (){}; //For functions
OR
var num = new Number();
var str = new String();
var bool = new Boolean();
var array = new Array();
var obj = new Object();
var myFunc = new Function();
This ensures that our variables are not undefined even when used without assigning other values rather the variables will only be empty. The first or explicit method is better than the object prototype method. It is easy and allows for good readability of codes. SUMMARY
  1. undefined is a data type and value assigned to variables without values or variables without values neither were they declared.
  2. When a variable is created and is being used without a value, it automatically becomes undefined.
  3. Same applies to functions and methods parameters. When no argument is passed on to a parameter during invocation. It becomes undefined.
  4. The typeof() operator will always return undefined for all variables in used without values.
  5. In order to ensure our variables are not undefined, we can decide to assign some default values of same data type with expected values to them even though those values may not be the supposedly intended values for those variables.
Take a short quiz on JavaScript Undefined.
Ask a question or tell us if JavaScript Undefined is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode