EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript undefined
 

TypeScript undefined

Updated June 2, 2023

TypeScript undefined

 

 

Introduction to TypeScript undefined

Whenever a variable is declared without assigning any value to it in TypeScript, then the variable is called undefined, so the variables that are declared without initializing a value to it are initialized as undefined, and this undefined can be assigned to variables of any data type, but undefined variables are not so useful because it can be assigned only one value that is undefined and no other value can be assigned to it, and they can be of use only when working with union types; hence there cannot be many cases where variables are declared and uninitialized to make them undefined variables.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax to declare undefined in TypeScript:

var variable_name;

Where variable_name is the name of the variable that is declared and is not initialized, thereby making it an undefined variable.

Working of undefined in TypeScript

  • A variable is declared and left uninitialized; then such a variable is called an undefined variable in TypeScript.
  • The undefined is applicable to variables of any data type.
  • The undefined variables can be of use only when working with union types; otherwise, they are useless because they can be assigned only one undefined value.
  • Hence there cannot be many cases where the variables are declared and uninitiated to make them undefined variables.

Examples of TypeScript undefined

Given below are the examples of TypeScript undefined:

Example #1

TypeScript program to demonstrate how declaring the variables and not initializing them can make it undefined.

Code:

//declaring a variable of type num without initializing value to it
let value:number;
//Displaying the value of the variable which is undefined
console.log("The value stored in the variable is\n");
console.log(value);

Output:

TypeScript undefined 1

In the above program, we are declaring a variable of type number without assigning any value to it. Then we are displaying the value of the variable, which is undefined. The output is shown in the snapshot above.

Example #2

TypeScript program to demonstrate how declaring the variables, initializing them, displaying their value as the output on the screen and explicitly assigning undefined to them can make them undefined.

Code:

//declaring a variable of type num and assigning the value 100 to it
let value:number;
value = 100;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable is\n");
console.log(value);
//explicitly assigning undefined to the variable
value = undefined;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable after explicitly assigning undefined is:\n");
console.log(value);

Output:

how declaring the variables, initializing them, displaying their value

In the above program, we are declaring a variable of type number and assigned the value 100 to it. Then the value of the variable is displayed as the output on the screen. Then we are explicitly assigning the value undefined to the variable. Then we are displaying the value of the variable, which is undefined. The output is shown in the snapshot above.

Example #3

TypeScript program to demonstrate how declaring the variables, initializing them, displaying their value as the output on the screen and explicitly assigning undefined to them can make them undefined.

Code:

//declaring a variable of type string and assigning the value EDUCBA to it
let value:string;
value = “EDUCBA”;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable is\n");
console.log(value);
//explicitly assigning undefined to the variable
value = undefined;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable after explicitly assigning undefined is:\n");
console.log(value);

Output:

explicitly assigning

In the above program, we are declaring a variable of type string and assigned the value EDUCBA to it. Then the value of the variable is displayed as the output on the screen. Then we are explicitly assigning the value undefined to the variable. Then we are displaying the value of the variable, which is undefined. The output is shown in the snapshot above.

Example #4

TypeScript program to demonstrate how declaring the variables, initializing them, displaying their value as the output on the screen and explicitly assigning undefined to them can make them undefined.

Code:

//declaring a variable of type float and assigning the value 1.234567 to it
let value:Float32Array;
value = 1.234567;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable is\n");
console.log(value);
//explicitly assigning undefined to the variable
value = undefined;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable after explicitly assigning undefined is:\n");
console.log(value);

Output:

TypeScript undefined 4

In the above program, we are declaring a variable of type float and assigned the value 1.234567 to it. Then the value of the variable is displayed as the output on the screen. Then we are explicitly assigning the value undefined to the variable. Then we are displaying the value of the variable, which is undefined. The output is shown in the snapshot above.

Example #5

TypeScript program to demonstrate how declaring the variables, initializing them, displaying their value as the output on the screen and explicitly assigning undefined to them can make them undefined.

Code:

//declaring a variable of type double and assigning the value 1.0e6 to it
let value:DoubleRange;
value = 1.0e6;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable is\n");
console.log(value);
//explicitly assigning undefined to the variable
value = undefined;
//Displaying the value of the variable as the output on the screen
console.log("The value stored in the variable after explicitly assigning undefined is:\n");
console.log(value);

Output:

TypeScript undefined 5

In the above program, we are declaring a variable of type double and assigned the value 1.0e6 to it. Then the value of the variable is displayed as the output on the screen. Then we are explicitly assigning the value undefined to the variable. Then we are displaying the value of the variable, which is undefined. The output is shown in the snapshot above.

Recommended Articles

We hope that this EDUCBA information on “TypeScript undefined” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. What is TypeScript?
  2. TypeScript Operators
  3. TypeScript Functions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW