Building Blocks of JavaScript


JavaScript is complete  high level client side and server side programming language now a days.
But most commonly used for writing client side scripts. So like other high level language it has its basic building blocks.

1. Variables for storing Strings and numbers
2. Operator like arithematic, logical and others
3. Basic Statements like if statement, if then else, switch, looping statements.

We will discuss all of them in this sections.

1. Variables for storing Strings and other data types.

As we know that a variable is name piece of memory where a data of our interest is stored for future use.

In JavaScript we can decalare variable like

var name = "Mark";

or

var FirstName;
FirstName=" Naveed Khan"

In statement var FirstName= " Mark" , var is reserved or keyword to decalare a variable. FirstName is variable name which is assigned the string values "Mark".

Var is keyword, or reseved word to declare a variable. Keywords or Resevred words are special words which have specific meaning for the interperater or compiler which are already defind by the vendor or inventor of the language. These words cannot be used for any other purpose.

= symbol is an assigment operator or assignment statement which assignes some values to the Variables.

"Mark" , Any thing which is enclosed inside the double quotes is treated as String in JavaScript. So Mark is string value which hold by the variable FirstName

Values of the variable is can be changed by assigning some other values like

FirstName= "Naveed Khan"



Now the value hold by the variable is First Name is Naveed.

2. Variable for Numbers

Similarly numbers can be assigned to the variables in JavaScript like as below

var salary=50000;

Here variable salary holds the values of 50000. Please note that in JavaScript there is no way to declare the special data types for variable. If a variable is assigned something like above without placing it inside the double quotation marks is treated as number in JavaScript.

Please note that every statement in JavaScript is ended with semicolon.

Rules for defining JavaScript variable names

1. A variable name cannot be placed inside the quoationg marks
2. The name can't be a number or start with a number.
3. It can't be any of JavaScript's keywords—the special words that act as programming instructions, like alert and var.
4. A variable name can't contain any spaces.
5. A variable name can contain only letters, numbers, dollar signs, and underscores.
6. Though a variable name can't be any of JavaScript's keywords, it can contain keywords. For example, userAlert and myVar are legal.
7. Capital letters are fine, but be careful. Variable names are case sensitive. A rose is not a Rose. If you assign the string "Floribundas" to the variable rose, and then ask JavaScript for the value assigned to Rose, you'll come up empty.
8. Use camelCase naming convention. Why "camelCase"? Because there is a hump or two (or three) in the middle if the name is formed by more than one word. A camelCase name begins in lower case. If there's more than one word in the name, each subsequent word gets an initial cap, creating a hump. If you form a variable name with only one word, like response, there's no hump. It's a camel that's out of food. Please adopt the camelCase convention. It'll make your variable names more readable, and you'll be less likely to get variable names mixed up.