Learn how to create the first program in typescript and print it in the command prompt
In this tutorial, we will learn how to create a variable using TypeScript and how to print a variable in TypeScript. We have written this tutorial with the consideration that you have already installed TypeScript, if not, kindly follow the link to install TypeScript in your system.
Getting started with TypeScript
Once the TypeScript is installed, you can check the TypeScript version from the command prompt. The coding to check the version is present in the above link.
Now, let’s see how to create a variable and print the variable in TypeScript. By default, we cannot see the output directly from the TypeScript; we will first run the TypeScript file using the command.
tsc filename.ts
The above command will generate the corresponding js file. Once created, we will run the js using the following code.
node filename.js
The above code will create the desired result. We can create a file with extension .ts from any of our code editors such as Brackets, Sublime, Notepad or Visual Code. After creating the ts file, now we can check how to create a variable and print the variable. To create a variable, we will type the following code.
let name1="John Doe"
The above syntax creates a variable “name1”, and we have assigned the value “John Doe” to it. After creating a variable, we need to print it. To print the variable, we will use the following code.
console.log(name1)
Now we will go to the command prompt to execute the TypeScript. Open the Command Prompt and navigate to the corresponding directory where you have stored the ts file. Then type
tsc demo.ts
Please note that demo.ts is the filename we have created for this program. The above command will generate a demo.js file automatically. We can execute only the demo.js file to view the output. You can see the difference in the coding.
var name1 = "John Doe";
console.log(name1);
Now we can run the js file by typing.
node demo.js
The above code will produce the output in the command prompt itself as follows.
John Doe
Now that you have created the First program in TypeScript, Try practising the first program in TypeScript. In the next tutorial, we will teach how to create an array in TypeScript and print it. Check our Angular with Angular Material Training from Ampersand Academy
Leave a Reply