Friday, June 3, 2016

Basics Concepts of JavaScript

Java script is a client side scripting language and it is standardize by ECMA. Its code is written between opening <script> and closing </script> tags. By using JavaScript we can create dynamic web page. JavaScript is embedded within HTML.
 
<script> tag has "type" attribute you must need to add in scripts tag like.

<script type="text/javascript">

There are three ways to write JavaScript code in your web page.
  1. embedded 
  2. external 
  3. inline 
In external way: there is src attribute like <script type="text/javascript" src="script.js(filepath)"> while in embedded we write JavaScript inside the html page enclosed in <script></script> tags.
 
Note: never use inline because you write this in html elements so very difficult to manage.

What are Client Side Languages?
Client side scripting languages are executed on any client machine instead of server so it doesn't take time to execute.

What are Server Side Languages?
Server-side scripting is used in web development which involves scripts on a web server which produces a response customized for each client's request to the website.

Variable
  • variable stores the value.
  • variable have some to define. 
1. variable name can be letter, number, any symbols.
var = x; var = 'bhumi'
2. variable name can't start with numbers it must start with letter.
3. variable name is case sensitive. Var and var both are different name
in JavaScript.
var = X; var = x; (both are different)
4. reserved keywords can't be given as variable name.
like function, switch, return etc.
5. logical operator[=, +, -, /] cannot use in variable name.
var = bh+bh;
6. blank space is not allowed within variable name.
var = 1 23;

Datatypes

The latest ECMAScript standard defines seven data types:

There are six primitive types of datatype.
1. Boolean
it has two values true and false.

2. null
it has one value null.
var a = null;
alert(a);//null
alert(a+'bhumi');//nullbhumi (concatenation)

3. undefined
variable has not assigned any value its undefined.
var a;
alert(a);//undefined
alert(a+'bhumi');//undefinedbhumi (concatenation)

4. number
variable has assigned by any number.
var a = 2;

5. String
variable has assigned by any latter.
var a = 'bhumi';

6. Symbol
(new in ECMAScript 6). A data type whose instances are unique and immutable.

Popups in JavaScript
There are three types of pop ups.
1. alert()
it is used for pop up alert message has one button is 'ok'.
alert('welcome to my website');//message

2. confirm()
it is used for conformation it has two button 'ok' and 'cancel'
confirm('Are you 18+');//ok and concel (Boolean = 0/1 or false/true)

3. prompt()
it is used to get vales from user.
prompt( 'Enter your age plz');

Function in JavaScript
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when it call.

Syntax:
function function name(para1, para2)
{
return();
}

Example:
<button onclick="main()">Click me</button>
function main(){
alert('hey');
}

Benefits of Function
  • You can reuse code: Define the code once, and use it many times.
  • You can use the same code many times with different arguments, to produce different results.
function main(greet){
alert(greet);
}
main('namaste');
main('hi');
main('hello');

nested function:
function contains other function call nested function. Such functions are very useful for abstraction and encapsulation.

var name = 'Bhumi';
function main(){
//local;
nested();
}
main();
function nested(){
alert(name);
}
nested();

Scope in JavaScript
Two types of scope in function.
1. local variable
local variable can be use within the function. you cant use it out side the function.

2. global variable:
global variable can use anywhere in the code.

Conditionals in JavaScript

if...else:
-> Use if to specify a block of code to be executed, if a specified condition is true
-> Use else to specify a block of code to be executed, if the same condition is false
-> Use else if to specify a new condition to test, if the first condition is false

switch :
it has many alternative code to execute.
switch(condition){
case ..
break;
}

ternary:

it has two values true and false

var getName = 45;
var result = (getName == 45)?'yes it is':'no it is not';
alert(result);

Loops are set of code which is used to repeat the condition of loop block. Loops returns the true or false values depend on the condition.
You can counter the loops by increments and decrements.

For Loop:

syntax:
for (statement 1; statement 2; statement 3) {
code block to be executed
}
for (var i = 0; i < 10; i++) {
console.log(i);//0,1,2,3,4,5,6,7,8,9
};

For in loop
When variable does not stored in array or variable have key pvalue pair then you can use for in loop.

Syntax:
for (var in object) {
code block to be executed
}
var myArr = {
name : 'Bhumi',
sname : 'Golakia',
age : 22
}
alert( myArr['name'] ); //Bhumi
for( var i in myArr ){
console.log("At key "+i+" got "+myArr[i]+" value.");
}

while loop:
This loop control the flow of code depend on condition. this loop checks the condition first and then it will do increments or decrements

syntax :
while (condition) {
code block to be executed
}
while ( i < 10 ) {
console.log(i); //0,1,2,3,4,5,6,7,8,9
i++;
};

Do while loops:
This loop first do the increment or decrements and the it checks the condition. Control flow statement that executes a block of code at least once.

syntax:
do {
code block to be executed
}
while (condition);
do {
console.log("before: "+i);//0,1,2,3,4,5,6,7,8,9
i++;
}
while ( i < 10 );

No comments:

Post a Comment

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...