Friday 14 October 2016

Beginners Approach to JavaScript Variables

Without wasting time, let me move to what variables are.Variables are containers for data items. Some of you will frown at the sight of this with the question “What does this statement mean?” Do not worry, I'll explain it.
Imagine you went to market and purchased a new backpack (some people call this bag) which you are to use in school (as a student). This backpack is a container (variable) and whatever school item you put in it, be it book(s) and/or pen(s) are the variable item(s).

How do I identify or make a variable in JavaScript?
JavaScript variables are declared using the var keyword, followed by the name of the variable. By convention,variables in JavaScript start with a lower case alphabet and every other word in the variable name starting with a capital letter, for example: var nameOfStudent;.This type of case is calledCamel case.
What characters can my variable name contain?
Variable names should never start with digits/a figure, but can contain figures — just not at the beginning. For example var 2letters; is wrong and will return an error, but var letter2; is a valid variable name.
Variable names can contain underscores, for example: name_of_school is a valid variable name, the underscore can be placed before the variable name for example var _boys;
A dollar sign can also start a variable name, for example var $name;
Variable names are case-sensitive, for example var x; and var X; are two different variables.
You must not use hyphens in your variable names as they are reserved for subtraction operations, and special symbols like #, % should not be in variable names.
“How do I put books in my ‘Bag’ ”
When you want to put your books inside your bag, you use the = sign, which is called the Assignment operator in most programming languages. Take for example I want to put "20" inside "numberOfBirds", I'll write:
var numberOfBirds = 20;
To assign "I am a boy" to "gender", I'll write this:
var gender = "I am a boy";
Got questions? Leave a comment here or send a message to 08132690608 on WhatsApp to get added to theJavaScript Geekiesgroup.

No comments:

Post a Comment

Your response mean a lot to us. Drop one.