TypeScript CLI Runner Online
Quickly test TypeScript code in a command-line-like environment—perfect for developers on the go.
🚀 total executions ( this month)
📚 Everyone's learning Typescript – what about you?
Loading...
💡 TypeScript Basics Guide for Beginners
1. Declaring Variables
TypeScript allows static typing with let
, const
, and type annotations.
let x: number = 10;
const pi: number = 3.14;
let name: string = "Alice";
let isActive: boolean = true;
2. Conditionals (if / switch)
Use if
, else if
, and else
for branching. switch
handles multiple values.
let x = 5;
if (x > 0) {
console.log("Positive");
} else if (x < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
switch (x) {
case 1:
console.log("One");
break;
case 2:
console.log("Two");
break;
default:
console.log("Other");
}
3. Loops
Use for
, while
, and for...of
for iteration.
for (let i = 1; i <= 5; i++) {
console.log(i);
}
let i = 5;
while (i > 0) {
console.log(i);
i--;
}
4. Arrays
Use square brackets []
to define arrays.
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob"];
console.log(numbers[1]);
5. Array Manipulation
Use built-in methods like push
, splice
, and filter
.
let arr = [10, 20, 30];
arr.push(40); // Append
arr.splice(1, 1); // Remove second element
console.log(arr);
6. Console Input/Output
Use console.log
to display output in the console.
const name: string = "Alice";
console.log("Hello, " + name);
7. Functions
Declare functions with typed parameters and return values.
function add(a: number, b: number): number {
return a + b;
}
console.log(add(3, 5));
8. Maps
Use plain objects or Map
for key-value structures.
const m = new Map<string, number>();
m.set("Alice", 25);
console.log(m.get("Alice"));
9. Exception Handling
Use try
and catch
to handle errors.
try {
throw new Error("Something went wrong");
} catch (e) {
console.error(e.message);
}
10. File I/O (Node.js only)
Use Node.js fs
module to read and write files.
import * as fs from "fs";
fs.writeFileSync("file.txt", "Hello File");
const content = fs.readFileSync("file.txt", "utf-8");
console.log(content);
11. String Manipulation
Use string methods like length
, concat
, includes
.
let str = "Hello World";
console.log(str.length);
console.log(str + "!");
console.log(str.includes("World"));
12. Classes & Objects
TypeScript supports object-oriented programming.
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
console.log(\`Hi, I'm \${this.name}\`);
}
}
const p = new Person("Alice");
p.greet();