×

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

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.

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 Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

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

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

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

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

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 radio button checked value

What are radio buttons? A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group...

5 minutes read.