Tuesday, 27 September 2016

JavaScript Basics

JavaScript is a dynamic programming language that, when applied to HTML document ,can provide interactivity on website.
What is dynamic behavior?
We do not need to define type of variable. It is automatically determined when a program is executed. 

Applications -
1.Creation of  games
2.Fully Blown database driven applications (MEAN development)
3.Animation using 2D,3D graphics.

JavaScript can be used for Front End Development (Rich Interactive UI) as well as Server End Development (with NodeJs).

Hence Full Stack Development of SPA(Single Page Application) is possible with JavaScript.
1.GMAIL (is SPA)
2.MEAN Development (MongoDB,Express,AngularJS,NodeJs)
.
Precisely except simple data types such as String, Number, Boolean, Null and Undefined
every other defined thing  in JavaScript is an Object such as Array, Functions etc. and such objects can be stored in variable
Comments-
1.Multiple lines - /*.........*/
2.single line - //This is comment.

Variables -
1. var name= "daniel";              //local or global variable depending on where it is declared.
2. name="daniel";                     //global variable declaration.
3. window.name="daniel";      //global variable declaration. (as same as 2)
4. const constant =100;            //constant variable declaration

Data Types -
A.Primitives 
1.String           var str = "tushar";
2.Number        var num = 100;
3.Boolean        var isBoolean = true ;
4.Null              var noValue = null;
5.Undefined    //When variable has no assigned value.
 B.Object
1.Object          All above examples.          //Every variable in JavaScript is Object.
2.Array            var arr = [''Tushar",12,"Anil",20];
3.Functions     function callMe(){return "hello"};

How to create Object in JavaScript?
1.var obj = {a:22};                                   //JavaScript Object Literal == JSON Object.
2.var arr = [11,"testMe",22];
3.var today = new Date();


Operators -
1.Add/Concatenation     var sum =2+3;  and  var joinStr = "Hello"+"Tushar";
2.Identity Operator(Strict)     ===         //Checks only whether two values are equal.
                                              ! or !==    //negation 
3.Identity Operator (Loose)   ==          //Converts data types when data types are not equal then checks
                                                           // values.
                                              !=           //negation
Hence always wise to use Strict operator to avoid data type conversion issues.

Conditional Operator (Ternary)- 
Used to assign value to variable based on conditions.
This is replacement for If-Else Loop.
Syntax- condition ? exp1(if true):exp2(if false)
Eg.
var ageGroup = age ? 1:0;   

//if age is true --> ageGroup = 1;
//else ageGroup =0;

Functions- 
How to define anonymous function?
var callMe = function(a){console.log(a*a); };   //function assigned for a variable.
calling function -->
callMe(5);

Output will be- 25.
 //This is as same as defining a function and calling it whenever needed. 

How to create Objects in JavaScript ?
JavaScript is Object based language.
Means, we do not create Class to create objects but we directly create Objects.

3 ways to create objects -
1.Object Literal
var emp = { id:007, name:"Tushar",salary:40000}   //Similar to Json Object

2.By creating instance of Object 
var test = new Object();

test.id =007;
test.name ="tushar";
test.salary =40000;

3.By using Object Constructor. 
function emp(id,name,salary){
this.id =id;
this.name=name;
this.salary=salary;
}


var test = new emp(007,"Tushar",40000);

JavaScript In-Built Objects - 
1.Object
2.String
3.Math
4.Function 
5.Array
6.Date etc. Hence JavaScript is called as Object Based language.

Browser Object - 
Window
Window is browser object and not JavaScript Object. 

JavaScript String Concatenation -
Note** - After First String occurrence all next + operator are treated as concatenation operators.
Eg.
var sum = 10+10+"30"  //sum is 2030
var sum ="10"+10+10   //sum is 101010

Difference between Client side JavaScript and server side JavaScript ? 
Client side javaScript is directly interpreted in Browser JavaScript Engine at runtime.
Server side javaScript is always complied before executing. 

 

 







 



 

No comments:

Post a Comment