JavaScript String and Date Objects

JavaScript String and Date Objects
JavaScript String and Date Objects

JavaScript String

The JavaScript string is a sequence of characters enclosed in double quotes. JavaScript Strings are basically Objects.

JavaScript strings can be created by two different ways

  1. By string literal
  2. By string object (using new keyword)

1) By string literal

The string literal in JavaScript is created using double quotes. It can take following :

var string1=”string value”;

example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var str="This is string literal";  
document.write(str);  
</script>  
</body>
</html>

2) By string object (by using new keyword)

The JavaScript string can also be created using new keyword is given below:

var string1=new String(“Hello India”);

The new keyword will create instance of string.

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var mystring=new String("hello World");  
document.write(mystring);  
</script>  
</body>
</html>

JavaScript String Methods

List of JavaScript string methods are shown below:

Methods Description
charAt() returns the position of char value present at the specified index.
charCodeAt() Returns the Unicode value of a character present at the specified index.
concat() It combines two or more strings.
indexOf() It provides the position of a char value present in the given string.
lastIndexOf() It provides the position of a char value present in the given string by searching a character from the last position.
search() Search method searches for   some designated expression in a given string and returns its position if a match occurs.
match() It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
replace() It replaces a given string with the specified replacement.
substr() This method fetches the part of the given string on the basis of the starting position and length.
substring() Substring method fetches the part of the a string on the basis of the specified index.
slice() Slice method only fetches the part of the given string based on  positive or negative index.
toLowerCase() It converts the given string into lowercase letter.
toLocaleLowerCase() It converts the given string into lowercase letter on the basis of host’s current locale.
toUpperCase() It converts the given string into uppercase letter.
toLocaleUpperCase() It converts the given string into uppercase letter on the basis of host’s current locale.
toString() It provides a string representing the particular object.
valueOf() It provides the primitive value of string object.
split() split method splits a string into multiple substring arrays and then returns that newly created arrays.
trim() It trims the white space from the left and right side of the string.

1) JavaScript String charAt(index) Method

The JavaScript String charAt() method returns the character at the given index.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var str="javascript";  
document.write(str.charAt(2));  
</script> 
</body>
</html>

Output: It will the ‘v’

2) JavaScript String concat(str) Method

The JavaScript String concat(str) method is used to join two or more strings.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="javascript ";  
var s2="Learning";  
var s3=s1.concat(s2);  
document.write(s3);  
</script>  
</body>
</html>

3) JavaScript String indexOf(str) Method

The JavaScript String indexOf(str) method returns the index position of the given string.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="javascript indexof method Example";  
var n=s1.indexOf("method");  
document.write(n);  
</script>  
</body>
</html>

4) JavaScript String lastIndexOf(str) Method

The JavaScript String lastIndexOf(str) method returns the last index position of the given string.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="javascript Lastindexof method Example";  
var n=s1.lastIndexOf("method");  
document.write(n);  
</script>  
</body>
</html>

5) JavaScript String toLowerCase() Method

The JavaScript String toLowerCase() method returns the given string to lowercase letters.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="JavaScript toLowerCase Example";  
var s2=s1.toLowerCase();  
document.write(s2);  
</script>  
</body>
</html>

6) JavaScript String toUpperCase() Method

The JavaScript String toUpperCase() method returns the given string to uppercase letters.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="JavaScript toUpperCase Example";  
var s2=s1.toUpperCase();  
document.write(s2);  
</script>  
</body>
</html>

7) JavaScript String slice(beginIndex, endIndex) Method

The JavaScript String slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="abcdefgh";  
var s2=s1.slice(2,5);  
document.write(s2);  
</script>  
</body>
</html>

Output:

cde

8) JavaScript String trim() Method

The JavaScript String trim() method removes leading and trailing whitespaces from the string.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var s1="     javascript trim    ";  
var s2=s1.trim();  
document.write(s2);  
</script> 
</body>
</html>

9) JavaScript String split() Method

JavaScript String split method splits a string into multiple sub string arrays, identified by space and returns the newly created array

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var str="This is JavaScript website";  
document.write(str.split(" ")); 
</script> 
</body>
</html>

JavaScript Date Object

The JavaScript date object is used to get year, month and day. A timer can be used on the web page with the help of JavaScript date object to display the date and time.

Date object have four constructors to create different date objects. It has methods to get and set day, month, year, hour, minute and seconds.

JavaScript Date Constructor

Date constructor creates date object in 4 different ways.

  1. Date()
  2. Date(milliseconds)
  3. Date(dateString)
  4. Date(year, month, day, hours, minutes, seconds, milliseconds)

JavaScript Date Methods

Methods Description
getDate() It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of local time.
getDay() It returns the integer value between 0 and 6 that represents the day of the week on the basis of local time.
getFullYears() It returns the integer value that represents the year on the basis of local time.
getHours() It returns the integer value between 0 and 23 that represents the hours on the basis of local time.
getMilliseconds() Returns an integer value between 0-999 counted as milliseconds on the basis of local time.
getMinutes() Returns an integer value between 0-59 counted as minutes on the basis of local time.
getMonth() It returns the integer value between 0 and 11 that represents the month on the basis of local time.
getSeconds() It returns the integer value between 0 and 60 that represents the seconds on the basis of local time.
getUTCDate() It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of universal time.
getUTCDay() It returns the integer value between 0 and 6 that represents the day of the week on the basis of universal time.
getUTCFullYears() It returns the integer value that represents the year on the basis of universal time.
getUTCHours() It returns the integer value between 0 and 23 that represents the hours on the basis of universal time.
getUTCMinutes() It returns the integer value between 0 and 59 that represents the minutes on the basis of universal time.
getUTCMonth() It returns the integer value between 0 and 11 that represents the month on the basis of universal time.
getUTCSeconds() It returns the integer value between 0 and 60 that represents the seconds on the basis of universal time.
setDate() It sets the day value for the specified date on the basis of local time.
setDay() It sets the particular day of the week on the basis of local time.
setFullYears() It sets the year value for the specified date on the basis of local time.
setHours() It sets the hour value for the specified date on the basis of local time.
setMilliseconds() It sets the millisecond value for the specified date on the basis of local time.
setMinutes() It sets the minute value for the specified date on the basis of local time.
setMonth() It sets the month value for the specified date on the basis of local time.
setSeconds() It sets the second value for the specified date on the basis of local time.
setUTCDate() It sets the day value for the specified date on the basis of universal time.
setUTCDay() It sets the particular day of the week on the basis of universal time.
setUTCFullYears() It sets the year value for the specified date on the basis of universal time.
setUTCHours() It sets the hour value for the specified date on the basis of universal time.
setUTCMilliseconds() It sets the millisecond value for the specified date on the basis of universal time.
setUTCMinutes() It sets the minute value for the specified date on the basis of universal time.
setUTCMonth() It sets the month value for the specified date on the basis of universal time.
setUTCSeconds() It sets the second value for the specified date on the basis of universal time.
toDateString() It returns the date portion of a Date object.
toISOString() It returns the date in the form ISO format string.
toJSON() It returns a string representing the Date object. It also serializes the Date object during JSON serialization.
toString() It returns the date in the form of string.
toTimeString() It returns the time portion of a Date object.
toUTCString() It converts the specified date in the form of string using UTC time zone.
valueOf() It returns the primitive value of a Date object.

JavaScript Date object Example

An example to print date and time with date object.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
Current Date and Time: <span id="txt"></span>  
<script>  
var today=new Date();  
document.getElementById('txt').innerHTML=today;  
</script>  
</body>
</html>

JavaScript Digital Clock Example

There are two ways to set interval in JavaScript: by setTimeout() or setInterval() method.

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body> Current Time: <span id="txt"></span>  
<script>  
window.onload=function(){getTime();}  
function getTime(){  
var today=new Date();  
var h=today.getHours();  
var m=today.getMinutes();  
var s=today.getSeconds();  
// add a zero in front of numbers<10  
m=checkTime(m);  
s=checkTime(s);  
document.getElementById('txt').innerHTML=h+":"+m+":"+s;  
setTimeout(function(){getTime()},1000);  
}  
function checkTime(i){  
if (i<10){  
  i="0" + i;  
 }  
return i;  
}  
</script>  
</body>
</html>

Javascirpt  String and Date object has been discussed here in details. If you have further queries feel free to comment. I will definitely get back to you to clear your doubts.

To visit the  Official website of JavaScript Click here

Similar posts : JavaScript Objects ,    JavaScript Functions and Loops    JavaScript Control Statements and Operators  Data Types in JavaScript

What are JavaScript String?

The JavaScript string is a sequence of characters enclosed in double quotes. JavaScript Strings are basically Objects.

What are JavaScript Date Objects?

The JavaScript date object is used to get year, month and day. A timer can be used on the web page with the help of JavaScript date object to display the date and time.
Date object have four constructors to create different date objects. It has methods to get and set day, month, year, hour, minute and seconds.
5 1 vote
Article Rating
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Santosh
Santosh
1 year ago

Well Explained

Simran
Simran
1 year ago

nice

trackback
1 year ago

[…] Reverse Charge Mechanism in Tally.ERP9            JavaScript String and Date Objects […]

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via
Copy link
Powered by Social Snap
%d bloggers like this: