EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript undefined
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript undefined

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,950 ratings)

Syntax to declare undefined in TypeScript:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

This is a guide to TypeScript undefined. Here we discuss the introduction to TypeScript undefined along with examples, respectively. You may also have a look at the following articles to learn more –

  1. Typescript
  2. What is TypeScript?
  3. TypeScript Operators
  4. TypeScript Functions
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more