Sprint 5
JavaScript
- JavaScript is the most popular programming language in the world
- Started out as a language to make websites interactive, now capable of building entire applications
- What is the difference between a website and a web application?
- High average salary for JavaScript developer
- JavaScript runs on browsers or outside of browsers with Node.js
- Two versions of JavaScript -> ES6 and ES5
Running JavaScript in the browser
JavaScript can be run in the browser by typing in the console. Right click and select “Insepct” to open the developer tools. Select the “Console” tab from the developer tools to open the JavaScript console. This is an interactive area where you can type JavaScript code to be executed by the browser.
- Open up the console, type
console.log('hello world');
and press enter - Try adding two numbers together
2 + 2
### Running JavaScript from and HTML page
Using the <script>
tag
- In an HTML file, add a
<script>
tag to the bottom of the body element. - Trying typing the
console.log('hello world');
code from earlier. - Open the page in your browser and open the console. You should see the same
hello world
message printed in the console.
Using a separate JavaScript file
- Create new file called
main.js
in your project folder - Copy the
console.log('hello world');
code from the<script>
tag into the newmain.js
file - Replace the
<script>
tag and the code inside with<script src="./main.js"></script>
- Reload the page to see if
hello world
still prints in the console.
JavaScript programming fundamentals
Variables
Variables are used to store a value for later use. To create a variable, use a special keyword to “declare” that you want a variable, then the name of the variable. To assign the variable to a “value” use an =
sign followed by the value.
In the video from last week’s homework, Mosh compared a variable to placing an object into a moving back and writing a label on the box. We can see what is in the box, and we can put something else inside of the box, but the label will always be the same.
Keywords:
let
-> declares a variable whose value will later changeconst
-> declares a variable whose value will never change (it’s “constant”)var
-> Old ES5 variable declaration
Variable name constraints:
- Cannot be a reserved word (let, const, if, for, …)
- Should be meaningful
- Cannot start with a number
- Cannot contain a space or hyphen
- Case sensitive
Primitive Types
There are a numebr of “Types” in JavaScript. Each “Type” uses a different syntax.
They are:
String
-> a set of characters surrounded by quotes- Example:
'Hello world'
- Example:
Number
-> an integer or decimal number- Example:
12
or1.2
- Example:
Boolean
-> a true or false value- Example:
true
orfalse
- Example:
null
-> a special value the represents and empty or unknown valueundefined
-> when a value has not been assigned
JavaScript also supports dymanic typing, meaning that variables can be re-assigned to different types.
Functions
Variables allow us to store values that can be used for later, functions allow us to store code that can be executed at a later time. Functions can also return
values which can be assigned to variables.
Function syntax:
function sayHello() {
console.log('Hello');
}
// call the function
sayHello();
// => 'Hello'
Functions become more powerful when used with parameters. Parameters are variables that are create by passing a value into a function.
Function with a parameter:
function sayHello(name) {
console.log('Hello ' + name);
}
// call the function
sayHello('Carson');
// => Hello Carson
Function with multiple parameters:
function area(width, height) {
return width * height;
}
const width = 4;
const height = 8;
const area = area(width, height);
console.log(area);
// => 32
Exercise 📝
Write a program in JavaScript that print your first and last name.
- Create a function called
printMyName
which accepts afirstName
andlastName
parameter printMyName
should console log your thefirstName
andlastName
separated by a space- Call your function,
printMyName
with your first and last names as parameters - Call your function again with a friend or family member’s first and last name as parameters
Conditionals: if
statements!
First we need to talk about expressions. Expressions are code statements that return a value. Here are a few examples:
2 + 2
// => 4
2 > 3
// => false
'hello' + ' ' + 'world'
// => 'hello world'
Remember the boolean type we talked about earlier? If statements are a way of running code only when a specific expression is true
. This is simple concept, that is incredibly powerful! The essence of programming is logic and running code conditionally.
Optionally, we can use and else
statement which will run when the if
statement is not true.
Example if
statement:
const height = 176;
if (height > 180) {
console.log("I hear you're pretty tall!");
} else {
console.log('Sorry');
}
// => "Sorry"
Exercise 📝
- Use the
prompt()
function to ask for a user’s height - Store the value of the prompt and pass it to a function which checks if is tall
- Print a message based on if they are tall or not (code above)
- 🚨Keep in mind variables types! Can we compare a string to a number?
Homework
- Finish the Intro to JavaScript video!!!
- Dig in to the Free Code Camp JavaScript challenges!