5 - JavaScript Scripting
BASH and other scripting languages are fantastic in the shell and for quick or OS-specific scripts. But what if you want something a little more powerful and cross-platform?
What are we learning?
In this lesson we will:
Review basic JS Syntax
Discuss the differences between NodeJS and browser JS
Install NodeJS in our environment
Write a couple JS scripts
Review of JavaScript
We've covered a lot already - let's take a moment to remind ourselves about the syntax and flow of JavaScript.
The grade 11 course covers the following:
You might also want to learn switch statements
The Lesson
Browsers have the brains of JavaScript already "installed". The interpreter and CLI are available throught the dev tools. (Try CTRL+SHIFT+J or CTRL+SHIFT+I or F12)
The Chrome version of that JS Interpreter has since been released as a standalone language and heavily modified for various use-cases. It works a lot like other scripting languages and has some significant differences with the browser version (no built-in prompt() as an example)
It is called NodeJS and it does not come pre-installed with Linux.
Let's Install NodeJS
From your terminal (CLI): sudo apt update && sudo apt install nodejs npm
That line will update the software repositories and then install the NodeJS software and the Node Package Manager. It might take a while - it's not small.
Our First NodeJS Script
Inside your Home folder (~) create a new folder for our work: mkdir js
Go into the folder (cd js) and make a new file called treats.js - we're going to write the dog treats program one last time - in JavaScript!
Note: to get the argument vector in NodeJS, we will use the process.argv array
Practice (Shake off the Rust)
Feel free to work together / with a partner on these. The goal is to problem solve while reviewing previous skills. You have today, tomorrow, and Thursday to work on these. If there is a snow day, we will lose that day.
Were those too easy for you? Try these competition problems.