JavaScript Basics



Like any programming language, JavaScript understands mathematical notations and uses operators like + (addition), - (subtraction), * (multiplication), and / (division) to manipulate numbers.

In your Inpector Console, use JavaScript to do some arithmetic. Who knew that your browser was a fully-fledged calculator?!


Variables are useful for storing data to be retrieved or manipulated later. To declare a variable, just type var followed by the name you'd like to assign the variable, then = and the value. For instance, if you wanted to create a variable called year and assign it the value of 1999, you could do so as follows:

var year = 1999

Or if you wanted to create a variable called course and assign it the value of IPT660, you could do so as follows:

var course = "IPT660"

Note that if your value is textual, then you need to wrap it in quotes, but numbers don't need quotes.

After you have done this, if you ever type the word year into the console, it will return 1999, and if you type course, it will return IPT660.

The word var is only used when we first declare a variable. Once the variable has been declared, you can then change it if needed. So, if we wanted to change the value of the course variable to IPT531, we'd just do the following:

course = "IPT531"

Now, if we type the word course, it will return IPT531 instead of IPT660 (which is now totally forgotten).

Logical Operators

In addition, JavaScript also can logically compare values with < (less than), <= (less than or equal to), < (greater than), >= (greater than or equal to), == (equal to), != (not equal to), && (and), and || (or). (Remember that the equals sign = does not compare values, because it is rather used in JavaScript to assign variables.)

In your Inspector Console, if you type 1<2, it will return true, while 1>2 will return false. You can combine these with the mathematical functions to compare complex values, such as 272*331>254*352.

Is this statement true or false 272*331>254*352?



Logical operators are not confined to numbers; they can also be used for comparing more complex pieces of data, such as text and objects. For instance, "cheese"=="crackers" would return false, while "cheese"!="crackers" would return true.

You can also combine variables with the mathematical and logical operators. For instance, if you type year + 10, the console will return 2009.

Logical Statements

You can use logical operators in conjunction with logical statements like if, else, and while to tell JavaScript what to do under different circumstances and for how long. For instance, since we declared the value of the year variable to be 1999, we can ask JavaScript to give us a warning if the course was taken longer than 10 years ago as follows:

var currentYear = 2023;
if(year<currentYear-10) {
  alert("The course is too old.");
} else {
  alert("The course is good!");


Functions are actions that you can store and run on data or objects many times. These are declared similar to variables and can then be referenced by their name and fed variables to run. For instance, let's take our previous statement and wrap it in a function called CheckYear as follows:

function CheckYear(year) {
  var currentYear = 2023;

  if(year<currentYear-10) {
    alert("The course is too old.");
  } else {
    alert("The course is good!");

Once you have done this, you can now call CheckYear() and feed it any value you like to see if a year is too old as follows:


You may do this with any value:


This content is provided to you freely by EdTech Books.

Access it online or download it at