×

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

Callback and Callback Hell in JavaScript

The widely used client-side programming language JavaScript is simple, light, and interpretive. JavaScript is used by the majority of client-side web applications. It is also possible to use JavaScript on...

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 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 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 getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute 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 Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

JavaScript p5.js

Introduction to p5.js P5.js is a JavaScript library for creative programming. It is built on Processing, a coding environment for creativity. Processing's major goal is to make it as simple as...

4 minutes read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

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

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 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 Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

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

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

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