The Grammar Hammer
About let
You use the let keyword to tell JavaScript
you are making a new variable. Once the variable is created,
you do not need to use let on that variable again.
In some older references, you will see var used
instead of let. You should use let instead.
Later we will see why this is a better way of doing things.
Variables are just symbols you store data under. The rules for variable names are simple.
- The first character must be alpha or an _
- Subsequent characters can be alphanumeric or _
You will often see camel notation in JavaScript, which sets off words
with capital letters. Example: numberOfItems. Your
python programming peers use snake notation that looks like
this: number_of_items. Use variable names that
are evocative of their roles.
Semicolons A semicolon in JavaScript is like a period in English. It means a sentence has come to a full stop. All of the statements you learn about in this initial part of the course are called worker statements and they are complete senetences.
Therefore, they should end with a semicolon.
Making and Running JS Programs.
console.log This function
prints stuff to the console. Let's do some exercises with
it. Make this file hello.js.
//Program helloWorld.js
console.log("Hello, World");
To run it do the following.
- Open
empty.htmlin your browser. - Copy the program.
- Paste the program into the console.
- Hit the enter key.
Often you will want to refresh the browser between runs, especially if you get error messages.
Some simple printing exercises
Problem 1 Can you think of two ways to print this? One uses a string method. The other does "the simplest thing possible."
* ** *** **** ***** ****** *******
Problem 2 Make this appear in the console.
Can you make a clever use of the repeat string
method?
*
* *
* * *
* * * *
* * * * *
* * * * * *
* *
* *
Problem 3Suppose you have these variables.
let animals = "Cows";
let food = "hay"
Can you print this text using +?
Cows eat hay!
Problem 4JavaScript has a special type of string called a format string We demonstrate it here.
x = 5
y = x*x
console.log(`The square of ${x} is ${y}.`)
What happens when you change the value of x?
Problem 5Can you use a format string to print "Cows eat hay" in the previous example?
Problem 6 Consider this array.
x = ["Je", "suis", "sans", "rainment"]
When you print it, it has no brackets on! Scandal! Write
a console.log statement that fixes this.
Problem 7 Here is an array.
let cats = ["spike", "tiger", "fluffy", "pansy"];
Print out its first and last entries. by saying, "First entry: spike, last entry: pansy" This should still work if we change the contents of the array. Of course, you may assume the array to be non-empty.
Problem 8 Here is another array consisiting of consecutive integers.
let cats = [1, 2, 3, 4];
Write a program that puts new integers at the beginning and end so it still has consecutive integers. In this case the array becomes
[0, 1, 2, 3, 4, 5]
See if you can repeat your procedure and grow the array a few more times.
Problem 9
Learn about the join() array method.
let x = [1,2,3,4,5];
Use it to print "1**2**3**4**5" from the variable x
we just created.