You are reading the article Working And Examples Of Typescript Undefined updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working And Examples Of Typescript Undefined
Introduction to TypeScript undefinedWhenever 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.
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 undefinedGiven below are the examples of TypeScript undefined:
Example #1TypeScript program to demonstrate how declaring the variables and not initializing them can make it undefined.
Code:
let value:number; console.log("The value stored in the variable isn"); 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 #2TypeScript 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:
let value:number; value = 100; console.log("The value stored in the variable isn"); console.log(value); value = undefined; console.log("The value stored in the variable after explicitly assigning undefined is:n"); console.log(value);Output:
Example #3TypeScript 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:
let value:string; value = “EDUCBA”; console.log("The value stored in the variable isn"); console.log(value); value = undefined; 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 #4TypeScript 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:
let value:Float32Array; value = 1.234567; console.log("The value stored in the variable isn"); console.log(value); value = undefined; console.log("The value stored in the variable after explicitly assigning undefined is:n"); console.log(value);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 #5TypeScript 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:
let value:DoubleRange; value = 1.0e6; console.log("The value stored in the variable isn"); console.log(value); value = undefined; 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 ArticlesWe hope that this EDUCBA information on “TypeScript undefined” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Working And Examples Of Typescript Undefined
Update the detailed information about Working And Examples Of Typescript Undefined on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!