Related post

Inheritance in Java programming with example | Zhullyblog

JavaScript variables: How to declare variables in JavaScript - Zhullyblog

JavaScript Variable



Hey, welcome to zhullyblog. If this is your first time here , please Subscribe to Zhullyblog by Email to get latest tutorial.



JavaScript Variables-How to declare variables in JavaScript


JavaScript variables are containers for storing data values. They are used when you want to store values and can be referred to later. To create (declare) a variable , you use the var keywords followed by the name given to the variable. The name given to the variable are called identifier. Once you have declared a variable , you assign a value to the variable.

This is an example


<script type="text/javascript" src="filename.js">
    var name=" Hassan";
    </script>

 


But please let me remind you that JavaScript is case sensitive so be careful with the use of word. We talked about case sensitivity  in JavaScript syntax.

As you can see , a variable is declared and the variable has a unique name and also a value assigned to it.
Well, that is just a simple example.  I said earlier that the names given to each variables are called identifier. An identifier can take a short name or a descriptive name but there are some rules applied to naming a variable which are :

 • Variable name can contain letter, digits , underscore or dollar sign.
  • Reserved words such as break, char ,enum , byte , boolean , long , const, return, true, bar , void , import , static , else , export etc cannot be used.
 • Variable Name must begin with a letter but an underscore or dollar sign can also begin the name but it is for professional JavaScript developers.
Don't worry, you will become one very soon.
  • Variable name are case sensitive. For instance Num and num are different.
So, var num is not the same as var Num.


JavaScript variables can hold both numbers and strings. This is an example



<script type="text/javascript" src="filename.js">
    var name=" Hassan";
    var age= 17;
    </script>

 

As you can see from the example above, strings are written in double quotation marks while digits are without quote.



Please share













JavaScript Syntax

Where do you place JavaScript in HTML document

JavaScript Strings: How to create and manipulate

JavaScript If, If else, If else if statement

JavaScript Function: How to define and call a function

How to generate output in JS

JavaScript Switch case


Comments