Friday, January 3, 2014

Introduction to JavaScript Series Part-1

In this series I am going to explain JavaScript. This series will be good for beginners who want to learn about JavaScript.

JavaScript - Birth

JavaScript was originally invented at Netscape. They introduced it as a light weight scripting language to interact with web pages and providing dynamic content to web pages. Original name of the language developed by Netscape was Livescript but later it was changed to JavaScript when Netscape added support for Java in their Netscape Navigator web browser. After its introduction, JavaScript was quickly adopted as client side scripting language for web pages. Later Microsoft introduced JavaScript support to its web browser. Soon after releasing JavaScript as a client side script language Netscape introduced it as a server side scripting language. Today JavaScript is the most popular programming language among the web developers. JavaScript standards are defined by ECMA script.

Introduction to JavaScript

As the name suggest JavaScript is not a programming language but it's a scripting language. However it's C like syntax makes it look like a traditional procedural language. JavaScript supports functions, dynamic objects, loosely typed variables, associative arrays, regular expressions, prototype inheritance and DOM support.  With features like this one can hardly call it “Just a Scripting Language”.
JavaScript is object oriented language. But here objects are dynamic. You can create objects at run time and add properties and methods to it. Also you can remove properties and methods from object at run time. This makes JavaScript objects completely dynamic in nature. In traditional object oriented programming language, classes are defined and objects are created from template of the class. JavaScript allows you to create object directly without a class. Objects can be created simply by adding their components like properties and methods. JavaScript is also a prototype based language. New objects can be created from existing objects.  JavaScript implements prototype inheritance in slightly different manner and this is the point where most of the programmers fail to understand object orientation in JavaScript.

JavaScript Syntax

Like other languages, JavaScript also have syntax that makes it completely structured language. JavaScript is case sensitive language.

Whitespaces

Whitespaces are generally referred to tabs, spaces and newlines used outside the string constant. In JavaScript whitespaces are normally used to separate tokens.

var foo = 'Hello World';
Here spaces between var and foo can’t be removed. Other spaces like space between foo and = and space between = and ‘Hello World’ can be removed. JavaScript uses semicolons as statement terminators. That is optional. If you don't insert semicolon but add new line between statements and if those statements are well formed, there will not be any error.

a = b + c
(d*e)

These statements are parsed as

a = b+c(d*e)
But only well formatted statements are parsed. So for a good practice it's recommended to use semicolon as statement terminators.

Comments

JavaScript support single line and multi-line comments.

//This is single line comment
/*This is
multi-line comment*/

Variables

Variables are declared with a var statement. In JavaScript variables does not have type attached to it so any value can be stored in variables.  Variable name must start with a letter or underscore (_) followed by any number of letters and digits. As mentioned earlier, JavaScript is a case sensitive language so var a and var A both are different variables. Variables name should not be one of the reserved keywords of JavaScript. Like other languages JavaScript variables also have scope.

var a=1;
function test(){
var b =2;
var c = a+b;
}
Scope of b variable inside a function is limited to its body while variable a declared outside the function has a global scope and it also can be accessed in a function.

Any uninitialized variable will have undefined value.

var a; //undefined
Null is used to define a variable with empty value.

var a = null;
Here undefined and null are primitive data types of JavaScript.

JavaScript Operators

JavaScript has set of arithmetic operators, assignment operators, conditional operators. Following are arithmetic operators.

  • + Used for adding  two numbers or to concat  two strings
  • - Used for subtraction of two numbers. 
  • * Used for multiplication of  two numbers
  • / Used for divison of two numbers
  • % Used for getting reminder of divison
  • ++ Used to increment variable value by 1
  • -- Used to decrement variable value by 1

Following are conditional operators.

  • == Equal to .Used to compare two values
  • === Used to compare two values and types
  • != Not equal to. 
  • !== Not equal value and type.
  • > Greater than
  • >= Greater than or equal to 
  • < Less than
  • <= Less than or equal to

No comments:

Post a Comment