Learn Deno and its Basics Part-1

Learn Deno and its Basics's picture

What is Deno?

Deno is a modern and secure runtime language for Javascript and Typescript. It is very similar to Node.js
The advantages of using Deno are:

  • Default security : By default Deno does not have access to the network, filesystem, and environment variables. We have to explicitly grant these permissions.
  • Web compatible : Deno has a set of Apis that are compatible with API web standards like fetch, FormData, URL, URLSearchParams, Worker, atob, btoa, and more.
  • Standard Library : Deno has a list of standard modules that are guaranteed to work with a specific Deno version such as UUID, hash, HTTP, and more. These modules are maintained by Deno maintainers.
  • Deno supports TypeScript out of the box.
  • Deno has built-in utilities such as formatted, linter, test runner, and document generator.

After knowing about Deno. Since Deno is considered a successor to the famous Node.js. Let's understand the difference.

What is the difference between Deno and Node.js?

these are the some common differences between these languages.you can see it on given table

Basis DenoNode
Enginev8v8
Host LanguageRustc++
Module syntaxESMCommon JS
APIsDeno global + Web Api + standard libraryBuilt-in modules
TypeScriptOut of the boxConfig
Top level async waitYesNo
Package ManagerNo import via URLnpm

Let's get our hands dirty now.

How to install Deno?

  • Go to Deno.land
deno land
  • Based on your operating system select the option.
  • I am using windows and I am using chocolatey. So I will use the command
    Choco install deno
  • After that, open PowerShell and run the command.
open PowerShell
  • Run deno –version to confirm you have successfully installed Deno. Please use PowerShell with administrative rights.

No Tools Or Config Required

Deno can run JavaScript or TypeScript out of the box with no additional tools or config required.
Examples.


JavaScript :

1 2 3 4 5 6 // app.js function sum(a, b) { return a + b } console.log(sum(3, 5)) // 8

TypeScript:

1 2 3 4 5 6 // app.ts function sum(a: number, b: number) { return a + b } console.log(sum(3, 5)) // 8

Deno Command Line Interface:

  • Command: deno help

This will provide you with important commands that can be used.

deno help

For eg: deno info, deno install, deno repl

Mostly we use deno run command that we use to run a file. We can also get all types of run commands by entering deno help run

Mostly we use deno run command that we use to run a file. We can also get all types of run commands by entering deno help run

 deno help run

This will give you more information about deno run commands. As you can see most of the run commands require permission.
Let's try some of the run commands that do not require permission. For this, we will write typescript code in the basic folder. I will create a file app.ts.
Run the basic hello world program.

Run the basic hello world program

Web API :

Deno comes with web api by default. In node, we don’t have a web API. Let’s see them in practice.

Prerequisites:

  1. Install deno for vs code extension
  2. In setting—>jsonsetting enter deno.enable =”true”
  • Fetch web Api in Deno
Fetch web Api in Deno

We have to give permission to allow network access deno run --allow-net app.ts
Here we are using web API to fetch data and then consoling in the program.

  • Blob

This is used to fetch large binary objects including media and files. We are just using array values to show you this example.

Blob

Similarly, we can use form data

Blob

More details of web API can be found here: https://deno.land/api@v1.28.1

Deno Specific APIs

Besides web APIs deno has specific APIs which generally starts with deno. Some of these apis are read and write files. Let’s see them in action.

Read Apis

There are several ways/APIs to read in deno. We are going to learn and talk about three of them.

1. Fn Deno.readTextFile

This is an asynchronous function so we have to use await in this. We have to give permission to read deno –allow-read app.ts

Fn Deno.readTextFile

Read text file return a promise with a string value.

2. Fn Deno.readFile

This is an asynchronous function so we have to use await in this. We have to give permission to read deno run –allow-read app.ts. But this will return the promise of value uint

Fn Deno.readFile

You can decode the text of the string using

1 2 3 4 const data = await Deno.readFile('./text.txt') const decoder = new TextDecoder('utf-8') console.log(data) console.log(decoder.decode(data))

PS C:\Users\4 way technologies\Deno\basic\basic> deno run --allow-read app.ts
Uint8Array(27) [
87, 101, 108, 99, 111, 109, 101,
32, 116, 111, 32, 68, 101, 110,
111, 32, 68, 101, 118, 101, 108,
111, 112, 109, 101, 110, 116
]
Welcome to Deno Development

  • Fn Deno.readAll

To read the file using this function we need to open the file first this also returns the promise value in uintarray8

1 2 3 4 const file = await Deno.open('./text.txt') const data = await Deno.readAll(file) Deno.close(file.rid) console.log(data)

With output

PS C:\Users\4 way technologies\Deno\basic\basic> deno run --allow-read app.ts
Uint8Array(27) [
87, 101, 108, 99, 111, 109, 101,
32, 116, 111, 32, 68, 101, 110,
111, 32, 68, 101, 118, 101, 108,
111, 112, 109, 101, 110, 116
]


Tanushree Pal's picture
Tanushree Pal

A science graduate who has a keen interest to lean about new technologies and research area. With an experience in the field of data analytics and content writing, she aims to share her knowledge among passionate tech readers.

Related Blogs

Learn Deno and its Basics Part-1

Deno is a modern and secure runtime language for Javascript and Typescript. It is very similar to Node.js

A brand new Slack platform in collaboration with Deno

This is the basis of collaborating with Deno and bringing in innovation in Slack.

A Beginner Guide To Deno Core Basic- Learn Deno 2023

Deno Js is a simple and well-designed runtime environment that is used for both JavaScript and TypeScript.

Share this Article

Page Content

What is Deno?

What is the difference between Deno and Node.js?

How to install Deno?

No Tools Or Config Required

Deno Specific APIs

Read Apis