3. JavaScript: Variables

Computers are always work in binary numbers 1 and 0 but humans work with decimal and words. To create a communication between humans and computers we need some medium which can translate human’s instructions into computer’s and computer’s language into human’s. This is the point where programming languages come into play. Programming languages allow human to write computer instructions in human readable form So it can be translated into machine understandable format.

One of the most important thing in any programming language including javaScript are variables (data types) where human can write some instructions to computer to handle different types of data including numbers, letters, words etc. Variables can be any type, it can be integer type which can hold age or score, it can be string type which can contain name or email address.

How to declare a variable

In javaScript, you can declare a variable with word “var”.

var name;
var emailaddress;
var email_address;
var age;

You can assign any value to the declared variable and you can change or update the value as well. Type of the variable depends on the value which you will assign to the variable. If you write

var name = “Khan”;
It will become string type variable, if you write

var age = 10
it will become integer type of variable.

You can declare boolean variable as well with
var myBoolean = true;
var myBoolean = false;

Add Strings variable

JavaScript provides very flexible way to add two strings which is quite simple and straight forward. Declare two string variables and add both variables with + operator.

<script type="text/javascript">
  // declare Strings
  var myvalue = "hello";
  var yourvalue = "hi";

  // add two strings 	
  var ourvalue = myvalue + " " + yourvalue;

Add Integers variable

Declare two integer variables and add them with + operator.

<script type="text/javascript">
 // declare integers or numbers
 var mynumber = 10.0;
 var yournumber = 20.0;

 var ournumber = mynumber + yournumber;

Boolean Variable

Boolean variable can have only one of two values, it can be true or false. Create a variable called ourboolean and assign it value true.
To keep it very simple, compare two numbers, if first value is greatar than second value, ourboolean value will get true otherwise false.

<script type="text/javascript">
  // declare boolean variable
  var ourboolean = true;
  ourboolean = 2 < 1;


Download file from GITHub