First JavaScript tutorial

This assignment takes you through an introduction to JavaScript, and culminates with these questions on Gradescope:

Note that all of the answers to the questions are embedded in the tutorial material below, plus the tutorials linked to below.

Getting Started with JavaScript

To get started with JavaScript, we’ll do a few exercises that are not connected with any web browser at all; just exploring JavaScript as a programming language.

While JavaScript was originally developed to be a scripting language for web browsers, with the introduction of Node in 2009, JavaScript began to be used outside of web browsers.

So, we’ll use node as our tool to interact with JavaScript.

Step 1: Getting Started with Node

In this section, there are some exercises that I encourage you to work through.

There is nothing for you to turn in. But if you skip over them, you may find that you are stuck on some of the later steps. These are each here to help you understand something you need later in the assignment.

I encourage you to read through this section, try the examples for yourself on CSIL or you own machine, and experiment with the code.

Step 1.1: Finding the node REPL

On CSIL, you can type node to enter a REPL (Read/Eval/Print Loop) for JavaScript. If you have node installed on your local machine, this will likely work there as well:

[pconrad@csilvm-03 ~]$ node
Welcome to Node.js v17.4.0.
Type ".help" for more information.
> 2 + 3
> "ucsb".toUpperCase()
> const schools=["UCSB","UCSD","Stanford"]
> schools.length
> schools[0]
> schools[2]
> schools[3]
[pconrad@csilvm-03 ~]$ 

To exit the node REPL, use Control-D.

Step 1.2: Hello World in JavaScript.

The “Hello World” program in JavaScript would look like this. Put this in a file called hello.mjs:

console.log("Hello, World!");

The m in mjs stands for Module. It allows us to use the import statement, and avoids errors such as following that we may get if we try to use import in a file that ends in plain old .js:

SyntaxError: Cannot use import statement outside a module

You’ll see later on that we don’t need the .mjs extension when working with front-end code in our web-apps; this appears to just be a standalone node thing. But we’ll keep it for now.

To run it, you can type: node hello.mjs:

[pconrad@csilvm-03 ~]$ cat hello.mjs 
console.log("Hello, World!");

[pconrad@csilvm-03 ~]$ node hello.mjs
Hello, World!
[pconrad@csilvm-03 ~]$ 

Step 1.3: Getting command line arguments

As you may know:

What about JavaScript under node.js? In this case, we access command line arguments thorugh the process.args.

Executing this one line program will show us how this works:


Let’s try passing in three command line arguments:

[pconrad@csilvm-03 try-node]$ node args.mjs foo bar fum
process.argv= [

As you can see:

Step 1.4: Classic for loops in JavaScript

The classic for loop in C/C++ and Java both is:

  for (int i=0; i<n, i++) {
     // do something with i

Let’s use a JavaScript for loop to print all of the command line arguments. Note that in place of int we have let.

Let’s put this into loop.mjs and try running it with and without command line arguments:

for (let i=0; i<process.argv.length; i++) {
  console.log("process.argv[" + i + "]=", process.argv[i]);

Here’s what it looks like when we run it:

[pconrad@csilvm-03 try-node]$ node loop.mjs foo bar fum
process.argv[0]= /usr/bin/node
process.argv[1]= /cs/faculty/pconrad/try-node/loop.mjs
process.argv[2]= foo
process.argv[3]= bar
process.argv[4]= fum
[pconrad@csilvm-03 try-node]$ 

Step 1.5: JavaScript string interpolation

Let’s focus on this line for a moment:

  console.log("process.argv[" + i + "]=", process.argv[i]);

There’s an easier way to write this using a feature called string interpolation. In any string that is delimited with backticks instead of double quotes, we can insert values by putting a dollar-size followed by an expression in braces, like this:


Same result, but much easier to read the code.

Try modifying your loop.mjs code in this way and run it again.

Step 2: Basic JavaScript tutorials

Please read through this JavaScript tutorial, designed specifically for React beginners.

Now, please read though this additional JavaScript tutorial:

Also read these shorter tutorials on specific topics:

These tutorials contain all of the information you should need to answer the questions in the Gradescope assignment: