Updated June 2, 2023
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.
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:
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:
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:
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:
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:
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.