Javascript - Hello world, variables, comments, operators, types
Learning goals
Hello world in javascript
console.log
Variables
const,letAssignment, reassignment
Giving variable names
Comments
Errors
Types
numberstringbooleantypeof
Operators
Arithmetic operators
Addition
+Subtraction
-Multiplication
*Division
/
Flipped classroom videos
Portfolio feedback
God responsive struktur
Virkelig imponeret over flex!
hr tag skal kun bruges til linjeskift ikke afstand
Tlf link
width og height atttributer skal ikke være i html på img tag
<img src="bla bla bla" height="500" width="190" alt="flower">nav tag skal wrappes i ul li
alt attributter skal beskrive indhold
<img class="portrait" src="Images/image.jpg" alt="image/portrait">,<img id="billede" src="image.jpg" alt="Billede">,alt="animation.gif"Brug ul li når det giver mening
Nogle har ikke fået deployer. Typisk fordi der ikke i root mappen er en
index.htmlfil. Fordi i har kaldt dem noget andetGode til at bruge ul og li, sektioner også
Fedt med kommentarer i css og html til at vise sektioner. Det er et rigtigt it-arkitekt mindset
/* -------------------------------- INTRODUCTION */Vildt højt niveau helt generelt!
Teacher instruction
Codelab kommer forbi idag
Syntax og problem solving
Create random pairs from two groups. People who have coded. People who have not coded. Tell about teaching a man to fish
The first 20 min we will work strictly with pair programming! 10 min each as driver and support. Only coding on one computer
After the 20 min the students decide if they would like to continue or work alone
If you continue pair programming remember to get time on the keyboard. Shifting who is driver.
I don't want to see anyone just supporting
I expect everyone to complete level 1 today
Meet at 11:20 to see some exercises
Peer instruction
Question 1
What will the following code log to the console?
i am 3 years oldi am 3years oldi am age years oldSyntax error
Question 2
What will the following code log to the console?
stringnumberboolean3Syntax error
Importing external javascript file in html
Let's write our first Hello world program in Javascript. Firstly create a new project in Webstorm. For javascript to run in the browser we need to import a javascript file in the index.html file. Like we did with the css (but the syntax of importing is different)
index.html
This line: <script src="main.js"></script> is the line that imports and runs javascript in the browser. The javascript that it will run can be found in the main.js file. The name of the javascript file does not matter.
main.js
console.log is a function that will log some text to the console. The text it will log in this example is Hello World
To run the javascript click on of the browsers when hovering over the index.html file.

To find the log go to your browser and open developer the developer tools. Depending on which browser you have it might be a bit different. Generally right click -> Inspect Element -> Then click on the Console or Konsol depending on your language. A shortcut on both mac and windows is F12.

📝 Exercise 1 - level 1
(This exercise will help you expand your understanding of console.log)
Log 4 statements like these to the console, but in different languages.
For example:
Exercise taken from CodeYourFuture
Variables
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.
Variable declaration
Let's first try and declare a let variable
Now we have declared (created) a variable called greeting. We have not put anything into it yet. Let's do that!
Variable assignment
Now we first declare the variable greeting and then on the next line assign it with the value of the text Hello.
This can be done in one line:
let vs const
let vs constVariables are declared with let and const keywords as follows.
lettells Javascript that the variable will be reassignedconsttells Javascript that the variable cannot be reassigned
Using let reassignment is possible:
Using sonst reassignment is not possible:
This will throw an error!
The rule to use is: When defining a variable, always declare it as a
const, then if you need to modify that variable change it toletAlways use
constuntil it won'tletyou 😉
Giving variable names
Giving good variable names is an art and we will get into that later aswell. For now try and give descriptive names that describes what the variable contains. Not what it is!
In javascript the way to give names is using camelCase
📝 Exercise 2 - level 1
Add a variable
greetingand assign a greeting of your choice to the variable. FxHello how are youPrint your
greetingto the console 3 times. You should see the greeting 3 times on the console, one on each line.
📝 Exercise 3 - level 1
Consider this code, it has a syntax error in it.
Fix it so that when running this file it logs the message 'I'm awesome!'
Exercise taken from HackyourFuture
Comments
Comments is text that describes the code. It is not interpreted by javascript (it is invisible to javascript). You can create a comment in two ways
Using
//will create a oneline commentUsing
/* COMMENT HERE */ will create a multiline comment
Try and use comments to document/describe your code
Errors
Working with errors is an integral part of working as a developer. Getting comfortable with getting errors, reading errors and understanding error is essential.
Let's start with this code

The error says Uncaught TypeError: invalid assignment to const 'name' main.js:2:1
invalid assignment to const 'name' tells us that we have made an invalid assignment to the variable name. main.js:2:1 tells us that the error happened at line 2. In this case the error happened because we are reassigning the const called name. We can either make the variable a let or not reassign the variable.
Types
In all programming languages there are different types of data. We will go through the most basic types which is string, number, boolean. These are also called the primitive types of javascript
Strings
Text is by software developers called strings.
String can be created in 3 different ways.
Using double quotes around the string
"hello"Using single quotes around the string
'hello'Using backpacks around the string
`hello`- string literal
String literal
A string literal is a special form of string where we can put a variable inside another string
There are a ton of string methods. Check them out here
String .length
.lengthTo get a number of characters of a string simply write .length after the variable
String concatenation
You can add two strings together
📝 Exercise 4 - level 1
Using string literals, string length and the following code. Log out a string that says: "My name is peter. It has 5 characters"
Numbers
There are different types of numbers in programming. These are the two most important
Integers - Whole numbers like 1, 45 or -7
Float - Numbers with decimals: 1.1, 100.5, 1.76
javascript does not care which you use. To javascript those are just numbers
Boolean
There is another primitive type in JavaScript known as a boolean value. A boolean is either true or false, and it should be written without quotes. A boolean is like a light switch that can be either on or off.
We can use boolean values to make decisions in our code based on certain conditions, as we shall see in the next class.
typeof
typeoftypeof is a helpful method that will help figure out what type most variables are.
📝 Exercise 5 - level 1
With pen and paper write down what the following will log out
Operators
An operator performs some operation on single or multiple operands (data value) and produces a result. There are different types of operators. Today we will talk about arithmetic operators (also called mathematical operators)
Addition
Subtraction
Multiplication
Division
📝 Exercise 6 - level 2
Create two variables
numberOfStudentsandnumberOfteachersand assign them to some numbers of your choosingLog a message that displays the total number of students and teachers
Expected Result
📝 Exercise 7 - level 2
Now log out the percentage of students and percentage of teachers
Exercise taken from https://syllabus.codeyourfuture.io/js-core-1/week-1/lesson#exercise-15-mins
Exercise 6.2 - problem solving
Turen fra Måløv til Vesterbro Station med S-tog tager 25 minutter. Strækningen er omkring 25 km.
Togene kører hver vej hvert 10. minut.
Hvis jeg kigger ud af vinduet fra måløv til Vesterbro, hvor mange tog kan jeg i gennemsnit forvente at se ud af vinduet?
📝 Exercise 8 - level 3
Part 1
Create a variable to store the name of your favourite pizza. Create a variable to store the price of the pizza
Now log at statement to the console that will show the pizza man the entire pizza order in a language he understands, eg. like this: New pizza order: <name of pizza>. The price of the pizza is: <price of pizza>
Part 2
Now we will modify the program so that you can order multiple pizzas
Create a new variable to store the amount of pizzas you would like to order
Now write a formula to calculate the total price of your pizza order, and save it in a variable called totalPrice
Modify the log statement for the pizza man so it shows the total price of the order
New pizza order: <amount of pizzas> <name of pizza>. Total cost for the order is: <total price>Try to change the price of the pizza and check if the total price is calculated correctly.
Research how the
prompt()functions works. Now use the prompt function to make the user write how many pizzas should be ordered
Exercise taken from HackYourFuture
Exercise 6.2 - problem solving
Kom med et overslag på hvor mange buschauffører bor i Storkøbenhavn?
📝 More exercises for experienced - level 3
Age-ify (A future age calculator)
Last updated