Hello world

Concepts

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

Overview

Deno is a secure runtime for both JavaScript and TypeScript. As the hello world examples below highlight the same functionality can be created in JavaScript or TypeScript, and Deno will execute both.

JavaScript

In this JavaScript example the message Hello [name] is printed to the console and the code ensures the name provided is capitalized.

Command: deno run hello-world.js

  1. /**
  2. * hello-world.js
  3. */
  4. function capitalize(word) {
  5. return word.charAt(0).toUpperCase() + word.slice(1);
  6. }
  7. function hello(name) {
  8. return "Hello " + capitalize(name);
  9. }
  10. console.log(hello("john"));
  11. console.log(hello("Sarah"));
  12. console.log(hello("kai"));
  13. /**
  14. * Output:
  15. *
  16. * Hello John
  17. * Hello Sarah
  18. * Hello Kai
  19. **/

TypeScript

This TypeScript example is exactly the same as the JavaScript example above, the code just has the additional type information which TypeScript supports.

The deno run command is exactly the same, it just references a *.ts file rather than a *.js file.

Command: deno run hello-world.ts

  1. /**
  2. * hello-world.ts
  3. */
  4. function capitalize(word: string): string {
  5. return word.charAt(0).toUpperCase() + word.slice(1);
  6. }
  7. function hello(name: string): string {
  8. return "Hello " + capitalize(name);
  9. }
  10. console.log(hello("john"));
  11. console.log(hello("Sarah"));
  12. console.log(hello("kai"));
  13. /**
  14. * Output:
  15. *
  16. * Hello John
  17. * Hello Sarah
  18. * Hello Kai
  19. **/