Swati Lathia

Learning ways

JavaScript Tutorial 3 – Built In Functions

String Functions

  • charAt()
  • It is a method that returns the character from the specified index.
  • Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.length – 1.
  • Syntax: string.charAt(index);
  • index − An integer between 0 and 1 less than the length of the string.
  • Return Value: Returns the character from the specified index.
<body>  
      	 <script type = "text/javascript">
         var str = new String( "This is string" );
         document.writeln("str.charAt(0) is:"+str.charAt(0)); 
         document.writeln("<br>str.charAt(1) is:"+str.charAt(1)); 
         document.writeln("<br>str.charAt(2) is:"+str.charAt(2)); 
         document.writeln("<br>str.charAt(3) is:"+str.charAt(3)); 
         document.writeln("<br>str.charAt(4) is:"+str.charAt(4)); 
         document.writeln("<br>str.charAt(5) is:"+str.charAt(5)); 
         </script>      
</body>
  • charCodeAt()
  • This method returns a number indicating the Unicode value of the character at the given index.
  • Unicode code points range from 0 to 1,114,111. The first 128 Unicode code points are a direct match of the ASCII character encoding. charCodeAt() always returns a value that is less than 65,536.
  • Syntax: string.charCodeAt(index);
  • index − An integer between 0 and 1 less than the length of the string; if unspecified, defaults to 0.
  • Return Value: Returns a number indicating the Unicode value of the character at the given index. It returns NaN if the given index is not between 0 and 1 less than the length of the string.
<body>  
	<script type = "text/javascript">
         var str = new String("Bhavan's H J Doshi Jamnagar");
         document.writeln(str.charCodeAt(0)); 
         document.writeln("<br>"+ str.charCodeAt(1)); 
         document.writeln("<br>" + str.charCodeAt(2)); 
         document.writeln("<br>" + str.charCodeAt(3)); 
         document.writeln("<br>" + str.charCodeAt(4)); 
         document.writeln("<br>" + str.charCodeAt(5)); 
         </script>      
</body>
  • concat()
  • This method adds two or more strings and returns a new single string.
  • Syntax: string.concat(string2, string3, …,stringN);
  • String2…stringN − These are the strings to be concatenated.
  • Return Value: Returns a single concatenated string.
<script type = "text/javascript">
var str1 = new String( "This is string one" );
var str2 = new String( " This is string two" );
var str3 = str1.concat(str2);      
document.write("Concatenated String :" + str3); 
</script>   
  • indexOf()
  • This method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.
  • Syntax: string.indexOf(searchValue[, fromIndex])
  • searchValue − A string representing the value to search for.
  • fromIndex − The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.
  • Return Value: Returns the index of the found occurrence, otherwise -1 if not found.
<script type = "text/javascript">
      var str1 = new String( "This is HJDoshi" );
      var index = str1.indexOf("is");
      document.write("indexOf found String :" + index ); 
      document.write("<br>");
      var index = str1.indexOf("Doshi");
      document.write("indexOf found String :" + index ); 
</script>
  • lastIndexOf()
  • This method returns the index within the calling String object of the last occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.
  • Syntax: string.lastIndexOf(searchValue[, fromIndex])
  • searchValue − A string representing the value to search for.
  • fromIndex − The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.
  • Return Value: Returns the index of the last found occurrence, otherwise -1 if not found.
<script type = "text/javascript">
        var str1 = new String( "This is HJDoshi" );
        var index = str1.lastIndexOf("is");
        document.write("indexOf found String :" + index ); 
        document.write("<br>");
        var index = str1.lastIndexOf("Doshi");
        document.write("indexOf found String :" + index ); 
</script>
  • slice()
  • This method extracts a section of a string and returns a new string.
  • Syntax: string.slice( beginslice [, endSlice] );
  • beginSlice − The zero-based index at which to begin extraction.
  • endSlice − The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string.
  • Return Value: If successful, slice returns the index of the regular expression inside the string. Otherwise, it returns -1.
<script type = "text/javascript">
	var str1 = new String("This is HJDoshi");
	document.write(str1.slice(-7,-1)); 
</script>  
  • split()
  • This method splits a String object into an array of strings by separating the string into substrings.
  • Syntax: string.split([separator][, limit]);
  • separator − Specifies the character to use for separating the string. If separator is omitted, the array returned contains one element consisting of the entire string.
  • limit − Integer specifying a limit on the number of splits to be found.
  • Return Value: The split method returns the new array. Also, when the string is empty, split returns an array containing one empty string, rather than an empty array.
<script type = "text/javascript">
         var str1 = new String("This is HJDoshi");
         document.write(str1.split(" ",3)); 
</script>  
  • toLowerCase()
  • This method returns the calling string value converted to lowercase.
  • Syntax: string.toLowerCase( )
  • Return Value: Returns the calling string value converted to lowercase.
<script type = "text/javascript">
      		var str1 = new String("This is HJDoshi");
      		document.write(str1.toLowerCase()); 
</script>
  • toUpperCase()
  • This method returns the calling string value converted to uppercase.
  • Syntax: string.toUpperCase( )
  • Return Value: Returns a string representing the specified object.
<script type = "text/javascript">
         	var str1 = new String("This is HJDoshi");
          	document.write(str1.toUpperCase()); 
</script>  

Math Functions

  • abs()
  • This method returns the absolute value of a number.
  • Syntax: Math.abs( x ) ;
  • x − A number
  • Return Value: Returns the absolute value of a number.
<script type = "text/javascript">
         var a = Math.abs(-10);
          document.write(a); 
</script>
  • ceil()
  • This method returns the smallest integer greater than or equal to a number.
  • Syntax: Math.ceil( x ) ;
  • x − A numbers.
  • Return Value: Returns the smallest integer greater than or equal to a number.
<script type = "text/javascript">
         		var a = Math.ceil(-4.3);
          		document.write(a); 
</script>
  • floor()
  • This method returns the largest integer less than or equal to a number.
  • Syntax: Math.floor( x ) ;
  • x − A numbers.
  • Return Value: Returns the largest integer less than or equal to a number x.
<script type = "text/javascript">
         		var a = Math.floor(-4.3);
          		document.write(a); 
</script>
  • log()
  • This method returns the natural logarithm (base E) of a number. If the value of number is negative, the return value is always NaN.
  • Syntax: Math.log( x ) ;
  • x − A numbers.
  • Return Value: Returns the natural logarithm (base E) of a number.
<script type = "text/javascript">
         		var a = Math.log(10);
          		document.write(a); 
</script>  
  • max()
  • This method returns the largest of zero or more numbers. If no arguments are given, the results is –Infinity.
  • Syntax: Math.max(value1, value2, … valueN ) ;
  • value1, value2, … valueN : Numbers.
  • Return Value: Returns the largest of zero or more numbers.
<script type = "text/javascript">
         		var a = Math.max(10,-3,0,200);
          		document.write(a); 
</script>
  • min()
  • This method returns the smallest of zero or more numbers. If no arguments are given, the results is +Infinity.
  • Syntax: Math.min(value1, value2, … valueN ) ;
  • value1, value2, … valueN : Numbers.
  • Return Value: Returns the smallest of zero or more numbers.
<script type = "text/javascript">
         		var a = Math.min(10,-3,0,200);
          		document.write(a); 
</script>
  • pow()
  • This method returns the base to the exponent power, that is, baseexponent.
  • Syntax: Math.pow(base, exponent ) ;
  • base − The base number.
  • exponents − The exponent to which to raise base.
  • Return Value: Returns the base to the exponent power, that is, base exponent.
<script type = "text/javascript">
         		var a = Math.pow(7,3);
          		document.write(a); 
</script>  
  • random()
  • This method returns a random number between 0 (inclusive) and 1 (exclusive).
  • Syntax: Math.random() ;
  • Return Value: Returns a random number between 0 (inclusive) and 1 (exclusive).
<script type = "text/javascript">
         var a = Math.random();
         document.write(a); 
</script>
  • round()
  • This method returns the value of a number rounded to the nearest integer.
  • Syntax: Math.round( x ) ;
  • Return Value: Returns the value of a number rounded to the nearest integer.
<script type = "text/javascript">
          var a = Math.round(-20.3);
          document.write(a); 
</script>
  • sqrt()
  • This method returns the square root of a number. If the value of a number is negative, sqrt returns NaN.
  • Syntax: Math.sqrt( x ) ;
  • x − A number
  • Return Value: Returns the square root of a given number.
<script type = "text/javascript">
         		var a = Math.sqrt(100);
          		document.write(a); 
</script>

Array Functions

  • concat()
  • It combines two or more arrays and returns a new string. This method doesn’t make any change in the original array.
  • Syntax: array.concat(arr1,arr2,….,arrn) 
  • arr1,arr2,….,arrn – It represent the arrays to be combined.
  • Return: A new array object that represents a joined array.
<script type = "text/javascript">
       		 var arr1=["C","C++","Python"];  
	     	 var arr2=["Java","JavaScript","Android"];  
		 var result=arr1.concat(arr2);  
		 document.write(result);  
</script>
  • join()
  • It combines all the elements of an array into a string and return a new string. We can use any type of separators to separate given array elements.
  • Syntax: array.join(separator) 
  • Separator() – It is optional. It represents the separator used between array elements.
  • Return: A new string contains the array values with specified separator.
<script type = "text/javascript">
       		var arr1=["C","C++","Python"];  
		var result=arr1.join();  
		document.write(result);  
</script>
  • pop()
  • It removes the last element from the given array and return that element. This method changes the length of the original array.
  • Syntax: array.pop() 
  • Return: The last element of given array.
<script type = "text/javascript">
       	var arr1=["C","C++","Python"];  
		var result=arr1.pop();  
		document.write("The deleted element = ",result,"<br>"); 
		document.write("Now the new array  = ",arr1);
</script>
  • push()
  • It adds one or more elements to the end of the given array. This method changes the length of the original array.
  • Syntax: array.push(element1,element2….elementn) 
  • element1,element2….elementn – The elements to be added.
  • Return: The original array with added elements.
<script type = "text/javascript">
        var arr1=["C","C++","Python"];  
	var result=arr1.push("Java","JavaScript");  
	document.write("The new length of an array = ",result,"<br>");
	document.write("Now the new array = ",arr1);
</script>  
  • shift()
  • It removes the first element of the given array and returns that element. This method changes the length of the original array.
  • Syntax: array. shift() 
  • Return: The first element of an array.
<script type = "text/javascript">
        var arr1=["C","C++","Python"];  
	var result=arr1.shift();  
	document.write("The deleted element  = ",result,"<br>");
	document.write("Now the array = ",arr1);
</script>
  • unshift()
  • It adds one or more elements to the beginning of an array.
  • Syntax: array.unshift() 
  • Return: The new length of an array.
<script type = "text/javascript">
        var arr1=["C","C++","Python"];  
	var result=arr1.unshift("ASP.net","Java");  
	document.write("The length of an array = ",result,"<br>");
	document.write("Now the new array = ",arr1);
</script>

Date Functions

  • The JavaScript date object can be used to get year, month and day.
  • getDate()
  • This method returns the day for the specified date on the basis of local time.
  • Syntax: dateObj.getDate()
  • getDay()
  • This method returns the value of day of the week for the specified date on the basis of local time. The value of the day starts with 0 that represents Sunday.
  • Syntax: dateObj.getDay() 
  • getFullYear()
  • This method returns the year for the specified date on the basis of universal time.
  • Syntax: dateObj.getFullYear()
  • getMonth()
  • This method returns the integer value that represents month in the specified date on the basis of local time. The value returned by getMonth() method starts with 0 that represents January.
  • Syntax: dateObj.getMonth() 
  • getHours()
  • This method returns the hour for the specified date on the basis of local time.
  • Syntax: dateObj.getHours() 
  • getMinutes()
  • This method returns the minutes in the specified date on the basis of local time.
  • Syntax: dateObj.getMinutes() 
  • getSeconds()
  • This method returns the seconds in the specified date on the basis of local time.
  • Syntax: dateObj.getSeconds() 
  • getMilliseconds()
  • This method returns the value of milliseconds in specified date on the basis of local time.
  • Syntax: dateObj.getMilliseconds() 
<script language="javascript">  
          var dt=new Date();  
          document.write(dt);
          document.write("Today's date ".dt.getDate());  
          document.write("Today is ".dt.getDay());  
          document.write("Current Year is ".dt.getFullYear());  
          document.write("Current Month is ".dt.getMonth());
          document.write("Current Hour is ".dt.getHours()); 
          document.write("Current Minute is ".dt.getMinutes());
          document.write("Current Second is ".dt.getSeconds()); 
          document.write("Current millisecond is ".dt.getMilliseconds());
</script>
Scroll to top