Basic Operators

Operators are used to perform operations with variables and arbitary values. This page explains the function of each operator and their syntax.

  • Arithmetic operators (+, -, *, /, %) - These are used to perform arithmetic with values stored either arbitarily or as variables. They can be performed by using their corresponding symbols as one would ordinarily do in pen-and-paper mathematics. They are addition (+), subtraction (-), multiplication (*), division (/) and modulus (%). Although other programming languages such as Microsoft Visual Basic and Visual C++ use the "Mod" keyword instead, JavaScript uses %, together with F#, among others.
  • Equals sign (=) - These can be used to assign values or strings to variables or can be used in conjunction with arithmetic operators. The result is always on the right hand side of the "=" sign and the variable is always on the left. An example is illustrated below:
var nineteen = 19;
var oneplusone = 1 + 1;
var sometext = "string";
  • Comparisons (==, !==, >=, <=, >, <) - These are used to compare strings, numbers or other variables. They return a true/false value. An example is shown:
var isthistrue = (5<9);
alert(isthistrue);
var isthisfalse = ("anyone"<"somebody");
alert(isthisfalse);

  • Logical operators (|| (or), && (and), ! (not)) - These are simple logic operators that work like:
var a = (true || false); //true
var b = (true || true); //true
var c = (false || false); //false
var d = (!true); //false
var e = (true && false); //false
var f = (true && true); //true
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License