| by Achyut Kendre | No comments

Java Script Built in Functions

Java Script functions
Java Script Functions

Java Script provides you built in methods for –

  • String Manipulation
  • Date Time Manipulation
  • Math Functions

String Manipulation Functions in JavaScript

JavaScript provides you following built in functions –

length :- is a property it gives you no of characters from string
toUpperCase():- convert the string to upper case.
toLowerCase():- convert the string to lower case.
concat() :- used to join/concate two arrays.
indexof(“string”):- will help you to find index no of specified string beginning.
lastindexof(string): – will help you to find index no of specified string from end.
slice(start, end) :- slice you to cut the part of string first start position to end position.
substring(start, end) :- sub string cut the part of string from start position to end position.
substr(start, length) :- cut the part of string from start position and no of characters.
replace(“old”,”new”) :- it will replace only first occurrence of string using new string.
charAt(position) :- it gives you the character at this position.
charCodeAt(position) :- it gives you the code of character at this position.

<html>
<head>
<title> select in javascript </title>
<script type="text/javascript">
 var str ="Hello this is string!";
 document.write("Length:"+ str.length+"<br>");
 document.write("Upper Case:"+ str.toUpperCase()+"<br>");
 document.write("Lower Case:"+ str.toLowerCase() +"<br>");
 document.write("Index of :"+ str.indexOf("t") +"<br>");
document.write("Last Index of:"+ str.lastIndexOf("t") +"<br>");
document.write("Slice:"+ str.slice(6,9) +"<br>");	
document.write("substring:"+ str.substring(6,9) +"<br>");
document.write("substr:"+  str.substr(6,9) +"<br>");
document.write("repalce:"+ str.replace("t","AH") +"<br>");
document.write("char at:"+ str.charAt(8) +"<br>");
document.write("char code At:"+ str.charCodeAt(8));
</script>
</head>
<body>
</body>
</html>

Math Functions in Java Script

Math.round() :- will round the number to integer number , it will take no to one step up if the no after . is five or greater than five and one step down if less than five.
Math.pow(no, power) :- will generate the power of no by power parameter.
Math.sqrt(no):- will get the square root of the given number.
Math.abs(no):- it will get you the positive value of the negative value.
Math.ceil(no): – it will take the no up to one step and remove the number after .
Math.floor(no):- it will take the no down to one step and remove the number after .
Math.min(n1,n2,n3 etc.) : – it will get you the minimum number from numbers passed as a parameter.
Math.max(n1,n2,n3 etc.):- it will get you the maximum number from the numbers passed as parameter.

<html>
<head>
<title> select in javascript </title>
<script type="text/javascript">
  var v=-45.6;
  document.write("Round:"+ Math.round(v) +"<br>");
  document.write("Pow:"+ Math.pow(3,5) +"<br>");
  document.write("Square Root:"+ Math.sqrt(100) +"<br>");
  document.write("ABC:" + Math.abs(v) +"<br>");
  var v1 =45.34;
  document.write("Ceil:"+ Math.ceil(v1) +"<br>");
  document.write("Floor:"+ Math.floor(v1) +"<br>");
  document.write("Sign:"+  Math.sin(34.45) +"<br>");
  var a=[45,86,67,2,4,145,56];
//  document.write("Minimum:"+ Math.min(12,45,15,6,17) +"<br>");
     document.write("Minimum:"+ Math.min(1234,45,56,67,78,91,12,34) +"<br>");
     document.write("Maximum:"+ Math.max(1234,45,56,67,78,91,12,34) +"<br>");
   document.write("random:"+  Math.random() +"<br>");
</script>
</head>
<body>
</body>
</html>

JavaScript Date Time functions

JavaScript provides you following date time functions to manipulate date and time.

new Date(): – Date object provides you new method to create date and you can use it as follows. You can create date object using var variable=new Date(); by default you will get current date in new date.
In JavaScript you should remember that month starts with 0 that means 0 Jan,1 march,2 April, 3 may, 4 June, 5 July etc. and same for day 0-Sunday, 1-Monday, 2-Tuesday etc.
new Date(year,month,day): – it will create the date object with specified year, month and day.

Get Functions – allow you to retrieve the data from date-time.
getDay():- will get you day of the week.
getDate():- will get you the date from the date
getMonth(): – will get you month of the date
getFullYear():- will get the year from date.
getHours():- will get you hours from the date.
getMinutes():- will get you minutes from the date.
getSeconds():- will get you seconds from the date.
getTime(): – it will get the time from the date.

Set Functions:- will allow you to set the value for various date properties.
setDate(date):- it will allow you to set the date in date time.
setMonth(month) :- it will allow you to get month from date time.
setYear(year) :- it will allow you to set the year from the date time.
similar way we have set for hours, minutes and seconds.

e.g. Date Time function example

<html>
<head>
<title> select in javascript </title>
<script type="text/javascript">
 var dt = new Date();
 document.write(dt +"<br>");
 document.write("Day Of Week:"+ dt.getDay() +"<br>"); 
 document.write("Date: "+ dt.getDate() +"<br>");
 document.write("Month:"+ dt.getMonth() +"<br>");
 document.write("Year:"+ dt.getFullYear()+"<br>");
 document.write("Hours:"+ dt.getHours() +"<br>");
 document.write("Minutes:"+ dt.getMinutes() +"<br>");
 document.write("Seconds:"+ dt.getSeconds() +"<br>");
 document.write("Time:"+ dt.getTime() +" <br>");
 dt.setDate(12);
 dt.setMonth(4);
 dt.setYear(2023);
 document.write(dt +"<br>");
var dt1 =new Date(2025,5,28);
document.write(dt1);
</script>
</head>
<body>
</body>
</html>