JavaScript and TypeScript
Overview of JavaScript and TypeScript
JavaScript is a dynamic, interpreted programming language widely used for web development. TypeScript is a superset of JavaScript that adds static typing, enabling developers to catch errors at compile time rather than runtime. This document will guide you through the key features and syntax of both languages.
Setting Up Your Environment
Before diving into coding, ensure you have the following installed:
- Node.js: JavaScript runtime for executing code.
- TypeScript: Install via npm:
- Visual Studio Code: A popular code editor with TypeScript support.
Basic Syntax and Commands
1. Variables
In JavaScript and TypeScript, you can declare variables using let
, const
, or var
.
let
: Allows you to declare variables that can be reassigned.const
: Declares variables that cannot be reassigned (though the object they point to can still be modified).var
: Function-scoped or globally scoped; its use is generally discouraged in favor oflet
andconst
.
Example:
2. Functions
Functions in TypeScript can have typed parameters and return types, improving code clarity.Example:
3. Interfaces
Interfaces are powerful for defining custom types and ensuring objects adhere to a specific structure.Example:
4. Classes
TypeScript enhances JavaScript classes with features like access modifiers and interfaces.Example:
5. Generics
Generics allow you to create reusable components that work with any data type while maintaining type safety.Example:
6. Working with Arrays and Tuples
TypeScript provides strong typing for arrays and tuples, allowing for more structured data handling.Arrays Example with Methods:
Tuples Example with Destructuring:
7. Enums
Enums are a way of giving friendly names to sets of numeric values.Example with String Enums:
8. Type Assertions
Type assertions allow you to specify a more specific type for a variable when you are confident about its type.Example with Non-null Assertion Operator (!):
9. Error Handling in TypeScript
TypeScript helps catch errors at compile time, making your code more robust.Example of Type Safety in Function Parameters:
Compiling TypeScript to JavaScript
To compile your TypeScript code into JavaScript:
- Create a file named
example.ts
. - Write your TypeScript code in it.
- Run the following command in your terminal:
- This will generate an
example.js
file that you can run using Node.js:
An example program based on JavaScript and TypeScript
JavaScript Sample Program
Task Manager in JavaScript
How to Run the JavaScript Program
- Copy the code into a file named
taskManager.js
. - Open your terminal and navigate to the directory where the file is saved.
- Run the program using Node.js:
TypeScript Sample Program
Task Manager in TypeScript
How to Run the TypeScript Program
- Copy the code into a file named
taskManager.ts
. - Open your terminal and navigate to the directory where the file is saved.
- Compile the TypeScript code to JavaScript:
- This will generate a
taskManager.js
file. - Run the compiled JavaScript program using Node.js:
Expected Output
Both programs will produce similar output when executed:
These sample programs illustrate how to manage tasks using classes in both JavaScript and TypeScript, showcasing their similarities and how TypeScript provides type safety. Happy coding!