Introduction Java Script and Java Script Fundamentals

Java Script Fundamentals


Introduction to Java Script

What is Java Script?
Java Script is Object Oriented scripting language, which is platform independent  (runs on different operating systems) and it is a lightweight scripting language runs inside a host environment i.e. web browser.
The client side Java Script extends the Java Script core language by deploying objects to control a web browser and it Document Object Model. On other hand server side Java Script extends the core language by using objects relevant to running Java Script on Server Side.

Java Script and Java

There are few similarities and distinctive features exist in Java Script and Java. The fundamental differences are:

1.Java Script is Object Oriented language where as Java is class based programming language

2.Java Script achieves inheritance by Proto type mechanism where as Java achieves inheritance by Class hierarchy. Prototypal inheritance enables Java Script objects to get dynamic inheritance through which objects can have features other what has been inherited. This makes an individual object unique.

3.Java is a Strongly Typed language where as Java Script is loosely typed language.

ECMAScript and Java Script

The core language of Java Script is derived from ECMAScript  (European Computer  Manufacturers Association). The ECMAScript has added majority of the language features like :

1.Java Script language syntax like parsing rules, control flow,keywords, object literal initialization.

2.Error or exception handling by implementing try/catch, throw and to create user defined error types

3.Java Script types ( Boolean,number,string,function,object)
4.Prototype-based inheritance

5.Use Strict mode.

6.Built in objects and function like, JSON,Math,Array,Object Introspection Methods and Prototype methods. Java Script interacts with various HTML, XHTML and XML web elements and Documents like buttons, checkboxes, radio buttons etc.

Fundamentals of Javascript

Java Script is case sensitive and uses Unicode Character Set.

Javascript Comments

// : Java Script single line comment

/*——-*/ : Java Script multiline comment

/* Java Script */ comments */ for better readability */ : Java Script does not allow nested comments as shown below, this throws SyntaxError

Javascript Declarations

Java Script has 3 types of variable declarations:

var declares a variable, it initializes a variable to a value.
Declares a block scope for local variable, it initializes a variable to a value.
Declares a read only named constant.

1. Var Variables are symbolic representation of values exist in Browser Memory or hard disk.

Java Script Rules for Variable Declarations:

1.Variable names are  identifiers

2.Identifiers should begin with a letter, underscore.

3.It can consist alphanumeric. But numbers should not be used in the beginning of the identifier.

4.You can used Unicode letters such as å and ü in identifiers Valid : _area , Triangle_Area, Invalid: 99, 01_area.

5.Varialbes declared using var or let statement with no initialization, will have always undefined as their value. var or let num;    Because no value is assigned to num, it has a default value as undefined. An attempt to access an undefined variable will throw ReferenceError Exception.

3 Ways of Declaring Variables

1. var number = 253 :  This declaration makes number as local variable with in a function scope where it exist. while(true){ var num = 1000;} document.write(num); // 1000

2. number = 1000 : This declaration makes number as global variable, because just assigning value to a variable without var or let elevates that variable to global scope.

3. let number = 100 : Let makes a variable available to entire block with in which it is declared. Let is used to declare the block level scope for variable. while(true){ let num = 1000;} document.write(num); // RefernceError num is not declared.

Javascript Variable Scope

All variables declared within a function is local variable. Such variables are accessed with in the function and not accessible outside the function, because they do not exist outside the function. All variables declared outside a function are global variables. Global variables belong to global object, i.e. window. These global variables exist in the entire window object memory. Hence, across the program and different windows global variables can be accessed.

Javascript Variable Hoisting

Variable hoisting is a unique behavior of java script variable, which allows script to use a variable before the variable being declared. document.write(z===undefined) // this gives raise to true. var z; The above script is equivalent to : var z document.write(z===undefined) // this gives raise to true.

Javascript Constants

Javascript allows you to declared an identifier with const keyword. The keyword const makes an identifier to be read only. Javascript variable follows same syntax as variable declaration. Constant has to start with a letter, underscore, dollar sign, and contain alphabetic, numeric or underscore characters.

Valid constant declaration : function multiply(){ consta pi = 3.14}
Invalid constant declaration : function multiply (){ consta pi=3.14, var pi // this throw an error }

Characteristics of Java script Constants

1. Java script constants have same block level scope as the Let variable. 2. Constant name can’t resemble with function or variable in the same scope.

Javascript-Dynamic Typing

Javascript is a loosely typed, in other words dynamic language. This feature enables the javascript runtime engine to consider the type of variable during the execution time. For example:
var num = 42
var num = 6.125
var num=”number”
are variables with similar name. But execution engine picks up the required variable, which is supposed to be process. In other words due to dynamically typed feature javascript does not want to declare the type of variable well ahead of its actual usage.

Javascript Data Structures and Data Types

Javascript has 6 primitive data types and 1 non primitive data type, they are :
1.Boolean : The value is always either true or false.
2.null : null is a special keyword, represents a null or no value.
3.undefined : It is primitive value automatically assigned to variables that are declared.
4.Number : It represents any integer or float . Integer – 42 float 3.1254. In fact it is 64 bit double precision 64 bit format.
5.String : It is collection of characters enclosed in single or double quotes.
6.Symbol : A symbol is a unique and immutable data type and may be used as an
identifier for object properties.
Non Primitive Data Type
7.Object :An Object is collection of attributes (variables) and behaviors (functions).Read more for further details.

Javascript Data Types Revisited


Numbers in Javascript are double precision 64 bit format. Hence, technically there is no Integer in Javascript. The following peculiarity is due to 64 bit format.
0.2 + .0.3 == 0.50000000000000000

parseInt() and parseFloat()

Javascript has built in global functions parseInt() and parsedFloat() to convert strings into integer and floating numbers.
Takes two arugments, first argument is a string being a number to be converted, second argument is base as shown in the following example :

parseInt(“123”, 10) : 123
parseInt(“010”,10) : 10

To convert a string into base of binary, just change the base as parseInt(“111”,2) : 7

if you pass a non numberic as a string to be converted into integer, parseInt() returns NaN (Not a Number)

parseFloat() too takes to arguments first being a string to be converted in to floating point number and by default the base is always 10, unlike parseInt() which takes any base as second argument.

Conversion by + unary operator

JavaScript enables you to convert all numeric string into numerical value by + unary operator.

+”444″ : 444
+ “0100” : 100
+ “x20”:20

parseInt(“hello”, 10) : NaN


Strings are sequenace of unicode characters enclosed either in single quotes or double quotes. Strings are objects and primitive data types in Javascript.

Properties of String:

String.prototype.constructor : Specifies the function that creates an object’s>

String.prototype.length : Reflects the length of the string.

String str = new String (“world”) // String object

str.length = 5 // length

As mentioned above String is an object has very useful functions like :


Returns the character at the specified index.


Returns a number indicating the Unicode value of the character at the given index.


Returns a non-negative integer that is the UTF-16 encoded code point value at the given position.


Combines the text of two strings and returns a new string.


Determines whether one string may be found within another string.


Determines whether a string ends with the characters of another string.


Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.


Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.


Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.


Used to match a regular expression against a string.


Returns the Unicode Normalization Form of the calling string value.


Boolean data type in java script has two values either true or false these two values are java script keywords.

. According to rules defined in java script following are by default converted boolean:

1. By Default these values are false
False, 0, empty string “”, NaN, null, undefined.
2. By default All other values are “True”.