×

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 Math

Math is build-in object that has properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers. Syntax varpi_val = Math.PI; varsin_val = Math.sin(30); Examples Math.pow() Math.pow(x,y) returns the value of x to the power...

2 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.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 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 Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 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.

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 Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 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 Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 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 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.