Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, 12 June 2016

Javascript - String

JavaScript strings are used for storing and manipulating text.It is an object that represents sequence of characters.

A JavaScript string simply stores a series of characters like "John Smith".
A string can be any text inside quotes. You can use single or double quotes.

Eg:
var name= "Bob";
var name= 'Bob';

String length
Below is the code for find the length of  a string

var name = "John Smith";
var val = name.length;

JavaScript String Methods
Let's see the list of JavaScript string methods with examples.

  • charAt(index)
  • concat(str)
  • indexOf(str)
  • lastIndexOf(str)
  • toLowerCase()
  • toUpperCase()
  • slice(beginIndex, endIndex)
  • trim()
charAt ()

var str="hellowworld";
document.write(str.charAt(4)); 

output: o

concat(str)

var str1="hello";
var str2="world";
var str3=str1.concat(str2);
document.write(str3); 

output: hello world

indexOf(str)

var str1="helloworld from javascript";
var num=str1.indexOf("from");
document.write(num);  

output: 11

lastIndexOf(str)

var str1="hellowworld from javascript";
var num=str1.lastIndexOf("java");
document.write(num);  

output: 16

toLowerCase()

var str1="JavaScript toLowerCase Example";
var str2=str1.toLowerCase();
document.write(str2); 

Output: javascript tolowercase example

toUpperCase()

var str1="JavaScript toUpperCase Example";
var str2=str1.toUpperCase();
document.write(str2); 

Output: JAVASCRIPT TOUPPERCASE EXAMPLE

slice()

var str1="abcdefgh";
var str2=str1.slice(2,5);
document.write(str2); 

output: cde

trim()

var str1="     javascript trim example    ";
var str2=str1.trim();
document.write(str2); 

output: javascript trim example

Wednesday, 8 June 2016

Javascript Array

JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
  1. By array literal
  2. By creating instance of Array directly (using new keyword)
  3. By using an Array constructor (using new key

1) JavaScript array literal

Below syntax is creating an array.
var arrayname=[value1,value2.....valueN];
Eg: var cars = ["Hyundai", "Volvo", "BMW"];

2) JavaScript Array directly (new keyword)

The syntax of javascript array using new keyword is as below.
var arrayname=new Array();  
Eg: var cars = new Array("Hyundai", "Volvo", "BMW");

3) By using array constructor

Here, you need to create instance of array by passing arguments in constructor .

Syntax:
var arrayname=new Array("param1","param2","param3")
Eg:
var emp=new Array("Hyundai","Volvo","BMW")



Wednesday, 27 April 2016

JavaScript Objects

A javaScript object is an entity having state and behavior (properties and method).
JavaScript is an object-based language. Everything is an object in JavaScript.
JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.

Creating Objects in JavaScript
There are 3 ways to create objects.

  1. By object literal
  2. By creating instance of Object directly (using new keyword)
  3. By using an object constructor (using new keyword)
1) JavaScript Object by object literal

Syntax:
object={property1:value1,property2:value2.....propertyN:valueN}  

2)By creating instance of object 
Syntax:
var objectname=new Object();  

3) By using an Object constructor
The this keyword refers to the current object.

Example:
<script> 
function student(id,name,age){
this.id=id;
this.name=name;
this.age=age;
}
st=new student(103,"Vimal Jaiswal",12);
 
document.write(st.id+" "+st.name+" "+st.age);
</script> 


Javascript Functions


JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Syntax:
function functionName([arg1, arg2, ...argN]){
 //code to be executed
}  

Example:
function firstFunction(x1, x2) {
    return x1 * x2;              // The function returns the product of x1 and x2
}


Function Arguments

We can call function by passing arguments. Let’s see the example of function that has one argument.


Example:
function getVal(num){
alert(num*num*num);
}  

Function with return value

Example:
<script> 
function getData(){
return "hello first program";
}
</script> 
<script> 
document.write(getData());
</script>

Monday, 4 April 2016

Javascript - Switch statement


The JavaScript switch statement is used to execute one code from multiple expressions. It is just like else if statement that we have learned in previous page.

Syntax for javascript switch statement

switch(expression){
case value1:
 code to be executed;
 break;
case value2:
 code to be executed;
 break;
......

default:
 code to be executed if above values are not matched;
}  



Example:
<script>
var color='C';
var result;
switch(color){
case 'A':
result="one";
break;
case 'B':
result="two";
break;
case 'C':
result="three";
break;
default:
result="No Color";
}
document.write(result);
</script>

Output: three  

Here the default keyword specifies the code to run if there is no case match

Monday, 22 February 2016

Javascript - Programs Basic

Javascript Programs
In a programming language, computer program instructions are called statements.
JavaScript is a programming language and JavaScript statements are separated by semicolons.

Example:
var a = 1;
var b = 2;
var c = a + b;

And next is the javascript statements, and it includes 

  • values
  • operators
  • expressions
  • keywords and 
  • comments

JS Values:
JS values are two types, literals (fixed values) and variables

Numbers - 10.50,100
String - with single or double quote"name"

Variables:
Variables are used to store data values. JS uses var keyword to assign variables.
Eg: 
var x;
x=10;

Operators:
JS uses an assignment operator ( = ) to assign values to variables: 
var a=90;
JS uses arithmetic operators ( + - *  / ) to compute values: 
a * 10 (expressions)
also in string level
 "Mark" + " " + "Layer", evaluates to "Mark Layer":

Comments:
Code after double slashes // or between /* and */ is treated as a comment.Comments are ignored, and will not be executed:

Eg:
var a = 50;   // I will be executed

// var a = 50;   I will NOT be executed

JS Statements:
JS statement tells the browser to write "I am in." inside an HTML element with id="example":
Example:
document.getElementById("example").innerHTML = "I am in.";

One program example:

var a = 5;
var b = 6;
var c = a + b;
document.getElementById("example").innerHTML = c;


Javascript introduction

Javascript is an object based scripting language. Javascript is lightweight and cross platform.It is a programming language for HTML and web.














JavaScript is used to create interactive websites. It is mainly used for:
  • Client-side validation
  • Dynamic drop-down menus
  • Displaying data and time
  • Displaying popup windows and dialog boxes etc

Basically JS can change the HTML content and below is one example.

document.getElementById("example").innerHTML = "Hello JavaScript";

This example uses the method to "find" an HTML element (with id="example"), and changes the element content (innerHTML) to "Hello JavaScript"

Javascript Example:


<h2>Welcome to JavaScript</h2>  
<script>  
document.write("Hello JavaScript - First example");  
</script> 

Output:

Hello JavaScript - First example