JavaScript, Made Fun for Beginners

Level up by doing with this interactive tutorial.

This tutorial was crafted for beginners. You'll travel from zero JavaScript know-how straight through to solving bite-sized JavaScript challenges once you wrap up.

No prior JavaScript experience is needed to jump in. You'll get to play around with code and see instant feedback without ever leaving your browser, right here on the

Pour a warm drink, settle in, and enjoy the

# Strings

Any bundle of characters (letters, numbers, symbols) is called a string. Every string has to open and close with quotation marks. You're free to pick single ' or double ", just be sure the same one shows up at the beginning and the end.

Those quotes are there to signal that the text wrapped inside is a value, and not

# Variables

The term "variable" literally means "can change"; they exist to store values that can change.

Picture a variable as a box with a sticky note attached to it. Reading the name written on that sticky note lets you reach whatever is tucked inside the box (variable). Much like the x and y variables you've seen in mathematics, they're just a friendly name that stands in for the data we want to point to.

Now let's grab the keyword var to declare (i.e. create) a variable named animal:

The keyword var is short for variable, while = tells JavaScript to tuck the value on the right-hand side into the variable on the left-hand side. Pick any variable name you like, just make sure it has no spaces.

Right after the equals = sign, type the string "praxide.com". Don't forget that every string has to be wrapped in quotes " "


These days, hardly anyone reaches for var to declare variables in JavaScript. The go-to choices are const (short for constant) and let. Throughout this starter tutorial, we'll stick with const.

Time to build a variable all on your own. Declare the variable food and give it the value "pizza". Reach for const rather than var.

# Numbers

Just like strings, numbers count as values too, only they never sit inside quotes. You can write them with or without decimals, and they may be positive or negative.

Whenever a number is written without any decimal point, we call it an integer.

Declare a variable called day and hand it the value 19.

# Operators

Operators are the little symbols sitting between values that let you perform actions such as addition +, subtraction -, multiplication *, division /, and more. JavaScript ships with dozens of operators, yet we'll zero in on the arithmetic operators, since those are the ones you'll lean on most when starting out.

Split 100 by 2.


Naturally, plenty of other operators exist too, such as modulus %, exponentiation **, increment ++, decrement --, and so on.

Reach for the + operator whenever you want to concatenate (join) two

  • Create the variable name.
  • Join firstName together with lastName (see how the N is capitalized).

Your solution should mirror exactly what you wrote in the example above, except this time you'll be joining (using the + operator) two strings together.

# Functions

Functions are reusable chunks of code that you can give a name. You feed them some data, they carry out a specific task with it, and then return a result.

Here's what a simple function looks like:

Any idea what it does? Let's break down each piece:

  • addTwoNumbers is the function's name here.
  • num1 and num2 are the parameters (that is, variables holding input data).
  • return is the keyword that leaves the function and sends back a value (output).

Getting the return keyword right really matters. Every time you meet a function, you have to return your answer, or else your code simply won't run.

The function below takes two parameters, each one holding a number.

  • Multiply the first parameter num1 with the second parameter num2.
  • Don't forget to use the return keyword, just like in the example above.

While functions typically accept parameters, they aren't actually required to.

Once the function hello() above is called, it will output "Hello World!".

# Arrays

Think of an array as a tidy row of items, like so:

Every fruit sitting inside that array is known as an element. Here they all happen to be a string, but an element can hold any value you like:

# Indexes

Every element in an array is tagged with an index that begins at 0. Back in our fruit example, "apple" sits at index 0, "banana" lives at index 1, "orange" is at 2, and the pattern rolls on to the end of the array.

When we want to grab a single element out of an array, we write:

That's exactly where "orange"

Give back the element "Donatello".

  • Keep in mind the very first element sits at 0.
  • Don't forget to return it!

# Mutability

The elements inside an array are mutable, meaning they can be changed whenever you need. Say, for instance, we have an array of numbers:

To swap out the value 14 (sitting at index 0), we write:

Given an array of numbers, update the value held at index 1 so that it becomes 88.

#.length

Most of the time you have no idea exactly how long an array is, or how many elements are packed inside it. That's precisely the moment the.length property saves the day.

Suppose we have an array of movie titles:

To find out exactly how long the movies array is, we reach for the.length property.

That hands back 4, since there are four elements sitting in the movies array.

Given an array arr whose size you don't know, return its length.

# Objects

Arrays are wonderful for simple lists, yet for certain jobs they become awkward to handle. Take an array of names:

Suppose you already know someone's last name (say it's Snow) and you'd like to find his first name. With arrays that's a real chore, because "Jon" lives in one array while "Snow" (his last name) sits in a completely separate one.

Things quickly turn messy, since adding one new person to the firstName array forces us to update the lastName array as well. And once we try to track more than just first and last names, it all gets truly tangled.

Objects give us a far simpler way to store and keep information tidy, like so:

Now each person has their own variable, which lets us pull out their values in a cleaner, far more readable fashion.

# Key-Value Pairs

Picture objects as a keyring where every key unlocks its own door, and beyond each door waits a room full of things. Label each key clearly, and you can fling open any door and grab whatever's inside.

Everything sitting to the left of the: are the keys, while everything on the right are the values. Together we call these key-value pairs.

Add the following key-value pairs into person4.

  • Assign firstName the value "Daenerys".
  • Assign lastName the value "Targaryen".
  • Assign email the value "dragonlady@gmail.com".

# Dot Notation

If you'd like to grab the lastName of person3, you would return person3.lastName. We call this dot notation because it relies on a. to point at the exact key you want to reach.

Use dot notation to grab the email of person.

# Loops

Loops give you a fast, simple way to repeat an action over and over. In this section we'll walk through the most common loop styles in modern JavaScript.

#.map()

The.map() method runs a function on every element inside an array. It then hands you back a brand new array. Put simply,.map() takes an array, transforms each element in it and returns a fresh new array.

Imagine we're working with an array of

If we want to multiply each element in the numbers array by 2, here's

That would give us:

Take 2 away from every element inside the numbers array.

#.filter()

The.filter() method hands back a new array holding every element that passes a test from a function you supply. Put another way,.filter() takes an array, checks each element and returns a new array holding only the elements that pass the test you provide.

Imagine we're working with an array of

To keep only the elements in the words array shorter than 5 characters in length, here's how:

Which hands back the following output:

Keep only the numbers in the years array that are greater than