Variables and Types

Collapse Content

In JavaScript, variables are defined using the var keyword, and can contain all types of variables.

We can define several types of variables to use in our code:

var myNumber = 3;                   // a number
var myString = "Hello, World!"      // a string
var myBoolean = true;               // a boolean

A few notes about variable types in JavaScript:

  • In JavaScript, the Number type can be both a floating point number and an integer.
  • Boolean variables can only be equal to either true or false.

There are two more advanced types in JavaScript. An array, and an object. We will get to them in more advanced tutorials.

var myArray = [];                    // an array
var myObject = {};                  // an object

Atop of that, there are two special types called undefined and null.

When a variable is used without first defining a value for it, it is equal to undefined. For example:

var newVariable;
print(newVariable); //prints undefined

However, the null value is a different type of value, and is used when a variable should be marked as empty. undefined can be used for this purpose, but it should not be used.

var emptyVariable = null;

will print out null


Define the following variables:

  1. A number called myNumber set to 4;
  2. A string called myString set to Variables are great.;
  3. A boolean called myBoolean set to false;

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us
Sign in or email us at [email protected]