diff options
author | Rob Waller <rdwaller1984@gmail.com> | 2020-08-06 16:35:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-06 11:35:08 -0400 |
commit | d7dcbab3efeeac5233c9cedb6edacc7202515449 (patch) | |
tree | 4c9a5c54a7b2c308937d94d4d6d3569daa1ccc08 /docs/examples/hello_world.md | |
parent | 24590b012f72ba1a02a5b90ef6c1156606a6ea53 (diff) |
docs: Improve examples (#6958)
Diffstat (limited to 'docs/examples/hello_world.md')
-rw-r--r-- | docs/examples/hello_world.md | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/docs/examples/hello_world.md b/docs/examples/hello_world.md new file mode 100644 index 000000000..90a833f71 --- /dev/null +++ b/docs/examples/hello_world.md @@ -0,0 +1,66 @@ +# Hello World + +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` + +```js +function capitalize(word) { + return word.charAt(0).toUpperCase() + word.slice(1); +} + +function hello(name) { + return "Hello " + capitalize(name); +} + +console.log(hello("john")); +console.log(hello("Sarah")); +console.log(hello("kai")); + +/** + * Output: + * + * Hello John + * Hello Sarah + * Hello Kai +**/ +``` + +## 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` + +```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("john")); +console.log(hello("Sarah")); +console.log(hello("kai")); + +/** + * Output: + * + * Hello John + * Hello Sarah + * Hello Kai +**/ +``` |