|
| 1 | +# **Bun Online Compiler** |
| 2 | + |
| 3 | +Write, Run & Share Bun code online using OneCompiler's Bun online compiler for free. It's one of the fastest, feature-rich online compilers for the Bun runtime. Getting started with OneCompiler's Bun editor is easy and fast. The editor shows sample boilerplate code when you choose Bun as the language and start coding. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +# **About Bun** |
| 8 | + |
| 9 | +**Bun** is a modern JavaScript runtime designed for speed, efficiency, and native support for modern web development. It is built on JavaScriptCore instead of V8, making it extremely fast for running JavaScript and TypeScript applications. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +# **Key Features** |
| 14 | + |
| 15 | +- **Blazing fast**: Bun is significantly faster than Node.js and Deno for many operations. |
| 16 | +- **Built-in package manager**: No need for `npm` or `yarn`—Bun can install packages directly. |
| 17 | +- **Native TypeScript & JSX support**: No additional configuration required. |
| 18 | +- **Embedded SQLite & Web APIs**: Makes it easy to build full-stack applications. |
| 19 | +- **Compatible with Node.js APIs**: Supports many existing Node.js modules. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +# **Syntax Help** |
| 24 | + |
| 25 | +## **Hello World** |
| 26 | +```typescript |
| 27 | +console.log("Hello, World!"); |
| 28 | +``` |
| 29 | + |
| 30 | +## **Variable Declaration** |
| 31 | + |
| 32 | +| Keyword | Description | Scope | |
| 33 | +|----------|---------------------------------|----------------------| |
| 34 | +| `var` | Old way of declaring variables | Function or global | |
| 35 | +| `let` | Block-scoped variable | Block scope | |
| 36 | +| `const` | Constant, cannot be reassigned | Block scope | |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## **Arrow Functions** |
| 41 | +Arrow functions provide a more concise syntax for defining functions. |
| 42 | + |
| 43 | +### **Syntax:** |
| 44 | +```typescript |
| 45 | +const greet = (name: string): string => `Hello, ${name}!`; |
| 46 | +console.log(greet("Bun")); |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## **Template Literals (Backtick Strings)** |
| 52 | + |
| 53 | +### **Interpolation** |
| 54 | +```typescript |
| 55 | +let name = "Bun"; |
| 56 | +console.log(`Hello, ${name}!`); |
| 57 | +``` |
| 58 | + |
| 59 | +### **Multi-line Strings** |
| 60 | +```typescript |
| 61 | +const message = ` |
| 62 | +Hello, |
| 63 | +Welcome to Bun! |
| 64 | +`; |
| 65 | +console.log(message); |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## **Arrays** |
| 71 | + |
| 72 | +An array is a collection of values. |
| 73 | + |
| 74 | +### **Syntax:** |
| 75 | +```typescript |
| 76 | +let numbers: number[] = [1, 2, 3, 4, 5]; |
| 77 | +console.log(numbers[0]); // Accessing first element |
| 78 | +``` |
| 79 | + |
| 80 | +### **Spread operator with Arrays** |
| 81 | +```typescript |
| 82 | +let moreNumbers = [...numbers, 6, 7, 8]; |
| 83 | +console.log(moreNumbers); |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## **Object Destructuring** |
| 89 | + |
| 90 | +### **Example:** |
| 91 | +```typescript |
| 92 | +const user = { name: "Alice", age: 25 }; |
| 93 | +const { name, age } = user; |
| 94 | +console.log(name, age); |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## **Loops** |
| 100 | + |
| 101 | +### **For Loop** |
| 102 | +```typescript |
| 103 | +for (let i = 0; i < 5; i++) { |
| 104 | + console.log(i); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### **While Loop** |
| 109 | +```typescript |
| 110 | +let count = 0; |
| 111 | +while (count < 5) { |
| 112 | + console.log(count); |
| 113 | + count++; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### **Do-While Loop** |
| 118 | +```typescript |
| 119 | +let num = 0; |
| 120 | +do { |
| 121 | + console.log(num); |
| 122 | + num++; |
| 123 | +} while (num < 5); |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## **Classes** |
| 129 | + |
| 130 | +Bun supports ES6 classes, allowing for object-oriented programming. |
| 131 | + |
| 132 | +### **Example:** |
| 133 | +```typescript |
| 134 | +class Car { |
| 135 | + model: string; |
| 136 | + |
| 137 | + constructor(model: string) { |
| 138 | + this.model = model; |
| 139 | + } |
| 140 | + |
| 141 | + display(): void { |
| 142 | + console.log(`Car model: ${this.model}`); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +const myCar = new Car("Tesla"); |
| 147 | +myCar.display(); |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## **Bun’s Built-in HTTP Server** |
| 153 | + |
| 154 | +Unlike Node.js, Bun has a **simplified** HTTP server API. |
| 155 | + |
| 156 | +### **Example:** |
| 157 | +```typescript |
| 158 | +import { serve } from "bun"; |
| 159 | + |
| 160 | +serve({ |
| 161 | + port: 3000, |
| 162 | + fetch(req) { |
| 163 | + return new Response("Hello, World from Bun!"); |
| 164 | + }, |
| 165 | +}); |
| 166 | + |
| 167 | +console.log("Server running at http://localhost:3000"); |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +Bun is an **exciting alternative to Node.js**, optimized for speed and modern JavaScript/TypeScript development! 🚀 |
| 173 | + |
0 commit comments