JavaScript – Variables and Datatypes

In JavaScript variable names MUST begin with a letter and they are case sensitive.

JavaScript variables stores:
– values
– expressions
– text values (strings)

How to declare a variable:

var x=5:

Notice the keyword – var -.

LOCAL VARIABLES
The variables declared inside a function becomes LOCAL and can only be accessed from within that function.
Local variables are deleted as soon as the function is completed.
You can have local variables with the same name in different functions.

GLOBAL VARIABLES
The variables declared outside a function becomes GLOBAL and they will die when you will close the webpage.

Try it yourself:

<!DOCTYPE html>
<html>

<head>
<title>Title</title>
 
<script language="javascript">
/* Types of variables and Data Types START */

// Short names ##############
var x=5;
var y=6;
var pi=3.14;
var a=123e5;      // 12300000
var b=123e-5;     // 0.00123

// Descriptive names ##############
var Age=7;
var age=8;

// Expression ##############
var z=x+y;

// Strings ##############
var customer="Rocky Joe";
var customer='Rocky Joe';
// Strings with quotes inside
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

// Booleans ##############
var t=true;
var f=false;

// Many variables in one statement (dichiarazione), separate the variables by comma ##############
var lastname="Tonin", hisage=39, job="web developer";

// Arrays - NOTICE Arrays are ZERO BASED ##############
var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
// Condensed Arrays
var cars=new Array("Saab","Volvo","BMW");
// Literal Array
var cars=["Saab","Volvo","BMW"];

// Condensed Object ##############
var person={firstname:"John", lastname:"Doe", id:5566};
// Multiple lines Object
var person={
firstname : "John",
lastname  : "Doe",
id        :  5566
};
// Adress the Object
name=person.lastname;
name=person["lastname"];
// Declaring variables as Objects using the keyword - new -
var name = new String;
var x =    new Number;
var y =    new Boolean;

// Undefined variables - variables with no value ##############
cars=null;
person=null;

document.write("Variables Rendering:" + "<br>");
document.write(x + "<br>");
document.write(y + "<br>");
document.write(pi + "<br>");
document.write(a + "<br>");
document.write(b + "<br>");
document.write(z + "<br>");
document.write(Age + "<br>");
document.write(age + "<br>");
document.write(customer + "<br>");
document.write(lastname + "<br>");
document.write(hisage + "<br>");
document.write(job + "<br>");
/* Types of variables and Data Types END   */

</script>

<noscript>
Your browser does not support JavaScript!
</noscript>
 
</head>
  
<body>
... this is the web page content ...
</body>

</html>

Conversions:

// ###################################################################
//  Convert a number to a string
// ###################################################################
var num = 15;
var n = num.toString(); // the result is 15, the value turs into a string now!

// ###################################################################
//  Convert a string to a number
// ###################################################################
var a = "1.45kg";
var n = parseFloat(a)  // the result is 1.45, the value turns into a number now!

// ###################################################################
// Convert a String to an Array - removing the commas, spaces, pipe 
// ###################################################################
var str="a,b,c,d,e,f";
var n=str.split(",");
alert(n[1]);                  // result is b! (the array index start at 0)

// Complete List
txt.split(",");   // Split on commas -> Great to read databases from a flat txt file!
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe 

My official WebSite >