Friday 16 September 2016

JavaScript Variables

jscript.png

Good day JavaScript Geekies! It's time for our next tutorial. Just like I said I'll be posting the tutorial here. No cause for alarm, you can ask your questions here as well as on WhatsApp. In this lesson we'll be discussing in brief, Variables in JavaScript.

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 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 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 called Camel 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 the JavaScript Geekies group.

4 comments:

  1. @"myName",

    That is a correct variable declaration. When you want to print the variable, you can just say: document.write(myName);

    Good job!

    ReplyDelete
  2. Using "document.write("myName");" is synonymous to var myName or do they play the same part in printing the variables or is the document.write is used in just displaying the declared variable only?

    ReplyDelete
  3. @olajide,

    var myName = "Webwiz" ;

    document.write(myName) ; will print Webwiz to the screen.

    document.write( "myName" ) ; will print myName to the screen.

    When printing , whatever you put in quotes will appear as-is, the ones without quotes will be treated as variables, checked if they exist and if this is true, their values will be displayed to the screen.

    ReplyDelete

Your response mean a lot to us. Drop one.