JavaScript is a scripting language used in browsers. It's one of the simpler languages out there, so let's get started!

Variables and basic syntax

First things first: you need to understand what a variable is. If you've ever taken an algebra class, this concept should be familiar to you. In algebra, a variable (usually x) is a letter that represents a variable. In JavaScript, a variable is a word that represents any kind of information.

In order to use a variable, you must declare it with the keyword var. Here's how you do it:

var x = 7;

A couple things to note here. First is that the word var precedes the name of the variable. The second is that the assignment of the variable is pretty much what you'd expect: x = 7. All that means is that the value of x is equal to the number 7. Third is that after the assignment, there's a semicolon. You know how in English, you usually end sentences with periods? Well, think of the assignment as a sentence. The semicolon is like a period; it tells the computer that your "sentence" is done.

So now, hopefully you understand what a variable is, and how you make one. Now consider the fact that a variable doesn't have to be a number. It can be a lot of things. For example, it can be text! In JavaScript, text is called "strings". It's the same syntax as before, but with one difference; you have to put quotation marks around the string. This is to let the computer know that you're talking about text, and not just the name of another variable.

var x = "Hello World!";

You can also set a variable to equal another variable, like so:

var x = 8;
var y = x;

Now y is equal to 8.

Community content is available under CC-BY-SA unless otherwise noted.