Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
JavaScript is not Java
Photo credits: Andrew E. Larson and John Seb Barber cc
Photo credit: Gillicious cc
Photo credit: Phil Synder cc
Image lightboxes
Fully featured web applications
Keep track of users with Cookies or storing data with local storage.
Interactive elements like tabs, sliders, etc
Drawing & animation
console.log('Hello World!');
alert('Boop!');
Computers are great at processing. They are bad at understanding.
When you write a program, you must break down every step into simple pieces.
Source: Harvard Students Making Sandwich: CS 50 Algorithm Intro
Photo credit: Dan Hatton cc
Photo credit: Adam Foster cc
Each instruction in JS is a "statement", like:
console.log('Hello World!');
console.log('I am glad to meet you');
console.log('I am fuzzy');
You can leave comments in your code—notes that people can read but computers will ignore.
/*
I can make long comments
with multiple lines here
*/
console.log('Hello World!'); // Or make short comments here
Open a popup box.
alert('Hello World!');
Display a message in your console.
console.log('Hello World!');
Photo credit: WJ van den Eijkhof cc
5 + 5
100 * 20
23 - 8
JavaScript understands integers...
10 * 10
// 100
...and floating point numbers
.5 + 1
// 1.5
JavaScript can't always figure out what you mean:
'what?' - 7
// NaN
NaN
= Not-A-Number
Example | Name | Result |
---|---|---|
-a |
Negation | Opposite of a . |
a + b |
Addition | Sum of a and b . |
a - b |
Subtraction | Difference of a and b . |
a * b |
Multiplication | Product of a and b . |
a / b |
Division | Quotient of a and b . |
a % b |
Modulus | Remainder of a divided by b . |
Use the console (right screen) to try out some operators.
Variables can be strings (groups of characters). You put your string in single or double quotes.
'Fluffy'
If you want to use a quote in your string, you'll need to escape it with a backslash.
'I\'d like to use an apostrophe'
Photo credit: Mike Lawson cc
You can put strings together with a +
, the concatenation operator.
'Kittens' + 'McDougle'
// KittensMcDougle
Remember that JavaScript doesn't know exactly what you want.
When combining strings, make sure to pay attention to white space
'Kittens' + ' ' + 'McDougle'
// Kittens McDougle
Just like 'x' in algebra, a variable is a named container for a value that can change.
Photo credit: giulia gasparro cc
To declare (create) a variable, just type the word var
and the variable name.
var numberOfKittens;
It is a good idea to give your variable a starting value. This is called initializing the variable.
var numberOfKittens = 5;
undefined
).$
, or _
.mycatsfullname
are hard to readvar myFavoriteMovie;
var numberOfViewings;
Once you have created a variable, you can use it in your code. Just type the name of the variable.
var numberOfKittens = 5;
console.log(numberOfKittens);
In your JavaScript file, create a variable and give it a valid name and value. Then, display the value.
var userName = 'Jane Lane';
var myAge = 30;
var catsAreBest = true;
var favoriteThings;
var goodPickupLines = null;
Create two variables, a first name and a last name, and then put them together to make a full name. Don't forget to display your results!
You can use concatenation to mix strings and numbers. When you do this, JavaScript will treat the number like a string.
var numberOfFruit = 6;
var typeOfFruit = 'bananas';
var allTheFruit = 'I have ' + numberOfFruit + ' ' + typeOfFruit + '!';
console.log(allTheFruit);
Create a program to calculate the tip at a restaurant. It should:
toFixed()
to round the bill total to 2 decimals.