On this page
Run a script
Deno is a secure runtime for JavaScript and TypeScript.
A runtime is the environment where your code executes. It provides the necessaryinfrastructure for your programs to run, handling things like memory management,I/O operations, and interaction with external resources. The runtime isresponsible for translating your high-level code (JavaScript or TypeScript) intomachine instructions that the computer can understand.
When you run JavaScript in a web browser (like Chrome, Firefox, or Edge), you’reusing a browser runtime.
Browser runtimes are tightly coupled with the browser itself. They provide APIsfor manipulating the Document Object Model (DOM), handling events, makingnetwork requests, and more. These runtimes are sandboxed, they operate withinthe browser’s security model. They can’t access resources outside the browser,such as the file system or environment variables.
When you run your code with Deno, you’re executing your JavaScript or TypeScriptcode directly on your machine, outside the browser context. Therefore, Denoprograms can access resources on the host computer, such as the file system,environment variables, and network sockets.
Deno provides a seamless experience for running JavaScript and TypeScript code.Whether you prefer the dynamic nature of JavaScript or the type safety ofTypeScript, Deno has you covered.
Running a scriptJump to heading
In this tutorial we'll create a simple "Hello World" example in both JavaScriptand TypeScript using Deno.
We'll define acapitalize
function that capitalizes the first letter of aword. Then, we define ahello
function that returns a greeting message withthe capitalized name. Finally, we call thehello
function with different namesand print the output to the console.
JavaScriptJump to heading
First, create ahello-world.js
file and add the following code:
functioncapitalize(word){return word.charAt(0).toUpperCase()+ word.slice(1);}functionhello(name){return"Hello "+capitalize(name);}console.log(hello("john"));console.log(hello("Sarah"));console.log(hello("kai"));
Run the script using thedeno run
command:
$ deno run hello-world.jsHello JohnHello SarahHello Kai
TypeScriptJump to heading
This TypeScript example is exactly the same as the JavaScript example above, thecode just has the additional type information which TypeScript supports.
Create ahello-world.ts
file and add the following code:
functioncapitalize(word:string):string{return word.charAt(0).toUpperCase()+ word.slice(1);}functionhello(name:string):string{return"Hello "+capitalize(name);}console.log(hello("john"));console.log(hello("Sarah"));console.log(hello("kai"));
Run the TypeScript script using thedeno run
command:
$ deno run hello-world.tsHello JohnHello SarahHello Kai
🦕 Congratulations! Now you know how to create a simple script in both JS and TSand how to run it in Deno with thedeno run
command. Keep exploring thetutorials and examples to learn more about Deno!