1 - Hello Javascript
Last updated
Was this helpful?
Last updated
Was this helpful?
Fork the to your personal account and clone it. Find help in the . This is the repo we will use during the first Javascript module, and for homework.
By the end of this class, you should be able to:
Use console.log()
to print information to the console
List and describe the different primitive data types in JS
Use typeof
to find the type of a variable
Assign data to variables using let
and const
Write, call and evaluate functions in JavaScript
Manipulate strings with concatenation and interpolation techniques
Manipulate numbers with mathematical operators using the Math
library
Define the difference between console.log()
and return
Call functions within functions
Search and read documentation to help when you are stuck
It is programming tradition that the first thing you do in any language is make it output 'Hello world!'.
We'll do this in JavaScript, using a command called console.log()
. The console.log()
method writes a message to the console.
The console is a tool which is mainly used to log information - it's useful for testing purposes.
(This exercise will help you understand how to run a basic JS script and explore the different ways you can run JS code)
Type console.log("Hello World!")
Run this script by going with the terminal to your directory and write node exercise-A.js
, you will see the result of the console.log in the terminal
(This exercise will help you expand your understanding of console.log)
Update the file exercise-B.js
script in the folder week-1/InClass
Write 10 statements like these, but in different languages.
For example:
When you write code, you'll want to create shortcuts to data values so you don't have to write out the same value every time.
We can use a variable to create a reference to a value. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container.
Before you use a variable in a JavaScript program, you must declare it. Variables are declared with let and const keywords as follows.
The program above will print "Hello world" to the console. Notice how it uses the value assigned to the variable greeting
.
Update the file exercise-C.js
script in the folder week-1/InClass
Add a variable greeting
to js1-week1.js and assign a greeting of your choice to the variable
Print your greeting
to the console 3 times. You should see your greeting 3 times on the console, one on each line.
In programming there are different types of data. You've used one data type already: string.
Computers recognise strings as a sequence of characters but to humans, strings are simply lines of text.
Notice that strings are always wrapped inside of quote marks. We do this so that the computer knows when the string starts and ends.
You can check that the data is a string by using the typeof
operator:
Update the file exercise-D.js
script in the folder week-1/InClass
Write a program that:
creates a variable called colors
assigns colors "blue" and "yellow" as a comma separate string to colors
logs the type colors
using typeof
.
What is the typeof
a number?
You can add two strings together using the plus operator (+
) or string interpolation.
String interpolation is a useful JavaScript feature that allows you to put variables directly into a string:
Update the file exercise-E.js
script in the folder week-1/InClass
Write a program that logs a message with a greeting and your name using the two concatenation methods we used
The next data type we will learn is number.
Unlike strings, numbers do not need to be wrapped in quotes.
You can use mathematical operators to calculate numbers:
Update the file exercise-F.js
script in the folder week-1/InClass
Create two variables numberOfStudents
and numberOfMentors
Log a message that displays the total number of students and mentors
Expected result
Numbers can be integers (whole numbers) or floats (numbers with a decimal).
Numbers with decimals can be rounded to the nearest whole number using the Math.round
function:
Update the file exercise-G.js
script in the folder week-1/InClass
Using the variables provided in the exercise calculate the percentage of mentors and students in the group (percentages must be a rounded to the nearest integer)
Using online documentation, what other things can you do with the Math
library? Pick one thing on your table that you can do other than Math.round
and prepare an explanation for the rest of the class
Expected result
Functions are blocks of code that can do a task as many times as you ask them to. They take an input and return an output.
Here's a function that doubles a number:
To use the function we need to: a) call it with an input and b) do something with the returned value, for example assigning it to a variable
The input given to a function is called a parameter.
A function can take more than one parameter:
When you write a function (sometimes called declaring a function) you assign names to the parameters inside of the parentheses (()
). Parameters can be called anything.
This function is exactly the same as the on above:
Update the file exercise-H.js
script in the folder week-1/InClass
Design and create a function that:
takes in more than one input
uses string concatenation
this means adding two strings together
performs some form of operation on a number
uses return
to return a string
Add a comment above your function to explain what it does
Call your function and run your script
What's the difference between a return
and console.log
?
When would you choose to use functions over the way we have been scripting so far?
Functions are very powerful.
You can write more than one line of code inside of functions.
You can use variables inside of functions.
You can call other functions inside of functions!
Update the file exercise-I.js
script in the folder week-1/InClass
Write a function that returns the year someone is born given their age as input
Using the answer from step 1, write a function that takes someone's name and age as input and returns a string that states the person's name and year they were born in a sentence
Console: a place on your computer to run scripts or commands from
Command: something that you type on your computer which performs an operation that your computer does
Directory: another word for "folder" on your computer
Parameter: a placeholder for values you can pass into functions
Primitive type: a built-in type in JavaScript (e.g. strings and numbers are primitive types in JavaScript)
Script: a file that contains a program
Terminal: another word for "console"
Update your first exercise-A.js
script in the folder week-1/InClass
in the you forked before