Learn Javascript – Variables & Datatypes
Hello everyone. In this post on JavaScript we are going to discuss variables and data types.
The first question that comes to mind is: what is a variable? A variable is a place where some value is stored. Put another way, a variable can be considered a container in which something is stored.
The identifier
An identifier is the name by which a variable or function is known. In JavaScript, any combination of letters, digits, underscores and dollar signs is allowed to make up an identifier.
Choosing variable names
One of the most important aspects of writing clear, understandable code is choosing appropriate names for your variables. For example:
var $ = "I am writing good code"; // not recommended
var _ = "This time it's fine"; // still not recommended
So what is the right way to declare a good variable? A variable’s name must give some information about what it is intended to do, or the purpose it will serve. A variable in JavaScript is declared with the var keyword followed by the identifier. Some good examples:
var name = "John Doe";
var shirt_name = "Peter England";
var pi = 3.14;
var cats = ["Tusy", "Joye", "Blue"];
var price = "$6.50";
But you don’t want to name variables as long as you wish:
var IwantToNamemyVariablelikethis90 = "Bad way to write a variable";
You can also use camelCase formats for naming a variable, or mix it with an alphanumeric format, and that will work just fine. Always remember to end your variable declaration with a semicolon.
Case sensitivity
JavaScript variables are case sensitive, so Monday, monday and MonDay are all different.
var Monday = "This is Monday";
var MonDay = "This is not the same";
var monday = "This is also not the same variable";
JavaScript will treat these three as different variables.
Variable minification and obfuscation
JavaScript developers often like to use short variable names — x, a, b, c, m1 and so on. This is mostly done to decrease the number of characters on the page. The process is called minification.
var m1, m2, m3;
So instead of using var yesterday, today, tomorrow; one can write minified variables instead.
Variable obfuscation can be seen in examples like:
var O110, O101, Oi01;
where O is a letter presented among numbers, which makes the reader think it might be a boolean value. Similarly:
var _$, __$$, _$$$;
is another way of obfuscating variables.
Variable scope
The scope of a variable is its accessibility from different parts of the program — in other words, where the variable is visible and available for use.
A variable has global and local scope. A variable visible from everywhere within a program is a global variable. A variable visible only from a specific context, such as a function, is of local scope. A context is a set of defined data created for a specific purpose.
Reserved keywords
Like all other programming languages, JavaScript has reserved keywords which cannot be used to name a variable — for example boolean, float, int, new and return.
Constants
JavaScript has a provision for constants, but ECMAScript Edition 5 does not support them across all browsers. A constant value can be made using the const operator.