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
What You'll Cover
# 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.
xxxxxxxxxx"praxide.com"xxxxxxxxxx'praxide.com'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:
xxxxxxxxxxvar animal = "Fox"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 " "
xxxxxxxxxxvar website = 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.
xxxxxxxxxx// enter code below this line# 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.
xxxxxxxxxxconst temperature = -7.5Whenever a number is written without any decimal point, we call it an integer.
Declare a variable called day and hand it the value 19.
xxxxxxxxxx// enter code below this line# 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.
xxxxxxxxxxconst division = 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.
xxxxxxxxxxconst firstName = "Luke "const lastName = "Skywalker"// enter code below this line# 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:
xxxxxxxxxxfunction addTwoNumbers(num1, num2) { return num1 + num2}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.
xxxxxxxxxxfunction multiplyTwoNumbers(num1, num2) { }While functions typically accept parameters, they aren't actually required to.
xxxxxxxxxxfunction hello() { return "Hello World!"}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:
xxxxxxxxxxconst fruit = ["apple", "banana", "orange", "mango", "tomato"]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:
xxxxxxxxxxconst stuff = [true, 1976, null, "hey"]# 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:
xxxxxxxxxxfruit[2]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!
xxxxxxxxxxconst turtles = ["Raphael", "Michelangelo", "Leonardo", "Donatello"]function turtlePower(turtles) { }# 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:
xxxxxxxxxxconst numbers = [14, 56, 78]To swap out the value 14 (sitting at index 0), we write:
xxxxxxxxxxnumbers[0] = 35Given an array of numbers, update the value held at index 1 so that it becomes 88.
xxxxxxxxxxconst numbers = [1, 4, 6, 8, 0]// enter code below this line#.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:
xxxxxxxxxxconst movies = ["The Matrix", "Se7en", "The Wizard of Oz", "Marry Poppins"]To find out exactly how long the movies array is, we reach for the.length property.
xxxxxxxxxxmovies.lengthThat 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.
xxxxxxxxxxfunction getLength(arr) { }# Objects
Arrays are wonderful for simple lists, yet for certain jobs they become awkward to handle. Take an array of names:
xxxxxxxxxxconst firstNames = ["Tyrion", "Jon", "Jorah", "Arya", "Joffrey"]const lastNames = ["Lannister", "Snow", "Mormont", "Stark", "Baratheon"]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:
xxxxxxxxxxconst person1 = { firstName: "Joffery", lastName: "Baratheon", email: "joff@widowswail.com"}const person2 = { firstName: "Jon", lastName: "Snow", email: "brooding@thewall.com"}const person3 = { firstName: "Tyrion", lastName: "Lannister", email: "tyrion@pourmeanother.com"}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.
xxxxxxxxxxconst person2 = { firstName: "Jon", lastName: "Snow", email: "brooding@thewall.com"}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".
xxxxxxxxxxconst person4 = { }# 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.
xxxxxxxxxxfunction getEmail(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
xxxxxxxxxxconst numbers = [1, 4, 9, 16]If we want to multiply each element in the numbers array by 2, here's
xxxxxxxxxxnumbers.map(x => x * 2)That would give us:
xxxxxxxxxx[2, 8, 18, 32]Take 2 away from every element inside the numbers array.
xxxxxxxxxxconst numbers = [1, 4, 9, 16]function subtractTwo(numbers) { }#.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
xxxxxxxxxxconst words = ["thaw", "achievement", "gain", "outlet", "difference"]To keep only the elements in the words array shorter than 5 characters in length, here's how:
xxxxxxxxxxwords.filter(word => word.length < 5)Which hands back the following output:
xxxxxxxxxx["thaw", "gain"]Keep only the numbers in the years array that are greater than
xxxxxxxxxxconst years = [1763, 1972, 1925, 1916, 1984, 1124, 1950, 2020]function getYears(years) { }