Avatar 💻 Developer by day, 🎮 gamer by night | Software Engineer in @Taboola | ☕ sometimes coffee lover | A good player in call of duty | 📷 sometimes love photography

Deno


What is Deno?

As per the definition, deno is a simple, modern and secure runtime for Javascript and Typescript which is built on V8, Rust and Tokio.

It has recently been announced the compatibility on MDN.

Feature

  • Supports Typescript
  • Ships a single executable
  • Has built-in utilities like a code formatter (deno fmt), a linter (deno lint), and a test runner (deno test).
  • Can bundle scripts into a single JavaScript file.
  • Set of reviewed standard modules that are guaranteed to work with Deno.

How to download Deno?

You can download from the official website.

Hello World with Deno!

Deno runs on Typescript, no additional config tools like webpack or bundle is required to compile. We can use both Javascript or Typescript with Deno.

I will be using Typescript to run a simple program
Command: deno run hello-world.ts:

/**
 * Hello-world.ts
 */

function capitalize(word: string): string {
    return word.charAt(0).toUpperCase() + word.slice(1);
}

function hello(name: string): string {
    return `Hello ${capitalize(name)}`;
}

console.log(hello('raj'));

/*
Output:

Hello Raj
*/


Let’s see more good examples with import modules
Command: deno run import-example.ts:

/**
 * import modules
 */

import { add, multiply } from 'https://x.nest.land/[email protected]/source/index.js';

function totalCost(outbound: number, inbound: number, tax: number): number {
    return multiply(add(outbound, inbound), tax);
}

console.log(totalCost(43, 22.5, 1.5));

/**
Output:
98.25
 */


Comparison to Node.js

  • Deno doesn’t use npm, it uses URLs or file paths.
  • Deno doesn’t use package.json.
  • All async actions in Deno return a promise, thus Deno provides different APIs than node.
  • Deno requires explicit permission for file, network or environment access which makes it more secure.
  • Deno dies in uncaught errors.
  • Deno uses “ES modules” and doesn’t support require() and third party modules are imported via URLs with above import module example:
import { add, multiply } from 'https://x.nest.land/[email protected]/source/index.js';


More information can be found in manual doc.



all tags