×

Javascript String

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript:
  1. String literal.
  2. Using new keyword.

String literal

By using double quotes string literal is created. Syntax
var stringname="string value";
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str="Hello String Literal";  
document.write(str);   
</script>  
</body>  
</html>
Try Now Output
Hello String Literal

Using new keyword

Syntax
var stringname=new String("new keyword")
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var stringname=new String("Hello String");  
document.write(stringname);  
</script>  
</body>  
</html>
Try Now Output
Hello String

String properties

There are list of properties of string object and there description.
  1. constructor.
  2. length.
  3. prototype.
Constructor- It returns a reference to the string function that created the object. Syntax
string.constructor
Length-It returns the number of characters in a string. Syntax
string.length
Prototype- It allowed to add properties and method to any object (Number, Boolean, string, and data etc.). This is global property which is available most of the objects. Syntax
object.prototype.name = value
There are methods also available in JavaScript string:
Methods Description
charAt(index) It returns the character at the given index.
concat(str) It merges the text of two string and returns new string.
indexOf(str) It returns the index position of the given string.
iastindexOf(str) It returns the last index position of the given string.
match() It is used to match regular expression against a string.
tolowercase() It is used to return given string value converted to lower case.
touppercase() It is used to return the given string value converted to uppercase.
valueOf() Returns the primitive value of the specified object.

charAt()

It is a method that returns the character from specified index.
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str = new String( "Hello" );  
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));   
</script>  
</body>  
</html>
Try Now

concat(str)

This method add two or more string and return a new string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Hello";    
var y="World";    
var z=x+y;    
document.write(z);    
</script>    
</body>  
</html>
Try Now Here another example of conct(str)
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var a = new String( "Hello" );  
var b = new String( "Friends" );  
var c = a.concat( b );  
document.write("Concatenated String : " + c);   
</script>  
</body>  
</html>
Try Now

indexOf(str)

String indexOf(str) method returns the index position of the given string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Tutorial provide by tutorialandexample";    
var y=x.indexOf("by");    
document.write(y);    
</script>    
</body>  
</html>
Try Now

lastindexOf(str)

String lastIndexOf(str) method returns the last index position of the given string.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="Tutorial provide by tutorialandExamole";    
var y=x.lastIndexOf("by");    
document.write(n);    
</script>    
</body>  
</html>
Try Now

match()

<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str = "To more detail, see Chapter 2.5.4.1";  
var re = /(chapter \d+(\.\d)*)/i;  
var find = str.match( re );  
document.write(find );   
</script>  
</body>  
</html>
Try Now

toLowerCase()

It returns the given string in lowercase letters.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="HELLO WORLD";    
var y=x.toLowerCase();    
document.write(y);    
</script>    
</body>  
</html>
Try Now

toUpperCase()

It returns the given string in uppercase letters.
<!DOCTYPE html>  
<html>  
<body>  
<script>    
var x="hello world";    
var y=x.toUpperCase();    
document.write(y);    
</script>    
</body>  
</html>
Try Now

valueOf()

It returns the primitive value of a string object.
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var str = new String("Hello world");  
document.write(str.valueOf( ));  
</script>  
</body>  
</html>
Try Now

Related Topics

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript Number

The Number object is a wrapper object which allows you to work with numerical values. The number object is created using the Number() constructor.It may be integer or floating-point. Syntax var n=new Number(value); Examples <!DOCTYPE html>   <html>   <body>   <script>   var x=100;//integer value   var y=100.7;//floating point value   var z=12e5;//exponent value, output: 1200000   var n=new Number(20);//integer value by number object     document.write(x+" "+y+" "+z+" "+n);   </script>   </body>   </html> Try Now Output 100 100.7 1200000 20 Description The primary...

1 minute read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

JavaScript setInterval()

On the Window and Worker interfaces, the setInterval() method calls a function or executes a snippet with a specified time delay between each call. This function provides an interval ID that...

4 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

Javascript Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

Javascript Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.