- JavaScript Basics
- JavaScript Home
- JavaScript Syntax
- JavaScript Placements
- JavaScript Output
- JavaScript Statements
- JavaScript Keywords
- JavaScript Comments
- JavaScript Variables
- JavaScript var
- JavaScript let
- JavaScript const
- JavaScript var Vs let Vs const
- JavaScript Operators
- JavaScript Arithmetic Operators
- JavaScript Assignment Operators
- JS Comparison Operators
- JavaScript Logical Operators
- JavaScript Bitwise Operators
- JavaScript Ternary Operator
- JavaScript Operator Precedence
- JavaScript Data Types
- JavaScript typeof Operator
- JS Conditional Statements
- JavaScript Conditional Statement
- JavaScript if Statement
- JavaScript if-else Statement
- JavaScript switch Statement
- JavaScript Loops
- JavaScript for Loop
- JavaScript while Loop
- JavaScript do-while Loop
- JavaScript Break Continue
- JavaScript Popup Boxes
- JavaScript Dialog Box
- JavaScript alert Box
- JavaScript confirm Box
- JavaScript prompt Box
- JavaScript Functions
- JavaScript Functions
- JS Function with Parameter
- JavaScript Return Statement
- JavaScript Variable Scope
- JavaScript setTimeout() Method
- JavaScript setInterval() Method
- JavaScript Events
- JavaScript Events
- JavaScript onclick Event
- JavaScript onload Event
- JavaScript Mouse Events
- JavaScript onreset Event
- JavaScript onsubmit Event
- JavaScript Objects
- JavaScript Objects
- JavaScript Number Object
- JavaScript Array Object
- JavaScript String Object
- JavaScript Boolean Object
- JavaScript Math Object
- JavaScript RegExp Object
- JavaScript Date Object
- JavaScript Browser Objects
- JavaScript Browser Objects
- JavaScript Window Object
- JavaScript Navigator Object
- JavaScript History Object
- JavaScript Screen Object
- JavaScript Location Object
- JavaScript Document Object
- JS Document Object Collection
- JS Document Object Properties
- JS Document Object Methods
- JS Document Object with Forms
- JavaScript DOM
- JavaScript DOM
- JavaScript DOM Nodes
- JavaScript DOM Levels
- JavaScript DOM Interfaces
- JavaScript Cookies
- JavaScript Cookies
- JavaScript Create/Delete Cookies
- JavaScript Advance
- JavaScript Regular Expression
- JavaScript Page Redirection
- JavaScript Form Validation
- JavaScript Validations
- JavaScript Error Handling
- JavaScript Exception Handling
- JavaScript try-catch throw finally
- JavaScript onerror Event
- JavaScript Multimedia
- JavaScript Animation
- JavaScript Image Map
- JavaScript Debugging
- JavaScript Browser Detection
- JavaScript Security
- JavaScript Misc
- JavaScript innerHTML
- JavaScript getElementById()
- JS getElementsByClassName()
- JS getElementsByName()
- JS getElementsByTagName()
- JavaScript querySelector()
- JavaScript querySelectorAll()
- JavaScript document.write()
- JavaScript console.log()
- JavaScript instanceof
- JavaScript Programs
- JavaScript Programs
- JavaScript Test
- JavaScript Online Test
- Give Online Test
- All Test List
JavaScript String
A string is a sequence of characters.
All the strings in JavaScript are represent as instances of the String object. You can use String object to perform operations on stored string like to find the length of the string.
JavaScript String Object Properties
The table given below describes the properties of String object in JavaScript.
Property | Description |
---|---|
constructor | gives the function, creates the prototype of a String object |
length | gives the length of a string |
prototype | adds properties and methods to an object |
JavaScript String Object Methods
Following table describes methods of the String object in JavaScript.
Method | Description |
---|---|
charAt() | gives the character in the specified index |
charCodeAt() | gives the Unicode equivalence of the character in the specified index |
concat() | joins two strings |
fromCharCode() | converts Unicode to character |
indexOf() | gives the position of the first occurrence of the specified character in a string |
lastindexof() | gives the position of the last occurrence of a specified value in a string |
match() | looks for the match between a regular expression and a string and returns the matches |
quote() | writes quotes in JavaScript |
replace() | searches for the match between a regular expression and a string, and replaces the matched substring with a new substring |
search() | searches for a match between a regular expression and a string, and returns the position of the match |
slice() | gives a part of a string as new string |
split() | splits a string into substrings |
substr() | extracts characters from a string beginning at the specified start index for the specified number of characters |
substring() | gives characters in a string between two specified indices |
toLowerCase() | converts a String value into a lowercase letter |
toSource() | returns an object literal representing the specified object |
toString() | returns a string representing the specified object |
toUpperCase() | converts a string into uppercase letter |
valueOf() | gives the primitive value of String object |
Here is an example of string in JavaScript:
var carname = "Audi A6"; var carname = 'Audi A9';
Here, both the strings are equal. To use single quote in a string, then quote the string in double quote like this:
var bupp = "It's a double quote"; var bupp = "He is a 'Computer Hacker'"; var bupp = 'He is an "Internet Hacker"';
Find Length of String in JavaScript
To find the length of the string in JavaScript, then use JavaScript built-in property length. Here is an example:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length;
Here the variable sln will hold the value, 26, which is the length of the string ABCDEFGHIJKLMNOPQRSTUVWXYZ
JavaScript Special Characters
As you know that, JavaScript string must be in quotes. Now let's look at the following code:
var y = "He is a "Computer Hacker" from New Delhi."
Here, the above string will be chopped to "He is a ". To avoid this problem, is simply to use \ escape character. Here is an example:
<!DOCTYPE html> <html> <head> <title>JavaScript String Object Example</title> </head> <body> <p id="string_para"></p> <script> var x = 'Today is Computer\'s World'; var y = "He is a \"Computer Hacker\" from New Delhi."; document.getElementById("string_para").innerHTML = x + "<br>" + y; </script> </body> </html>
Here is the output produced by the above String object example program in JavaScript.

Here is the live demo output, you will watch in your browser, produced by the above JavaScript String object example code.
JavaScript Special Character List
Character Code | Output |
---|---|
\n | newline |
\t | tab |
\r | carriage return |
\' | single quote |
\" | double quote |
\\ | backslash |
\f | form feed |
\b | backspace |
JavaScript String Object Example
Here is an example of String object in JavaScript.
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>JavaScript String Object Example</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> function student(stud_name,stud_branch,stud_marks) { this.stud_name=stud_name; this.stud_branch=stud_branch; this.stud_marks=stud_marks; } var stud1=new student("Deepak Patel","CSE",927); document.write("<B>Name</B> : "+stud1.stud_name+ "<BR/> <B>Branch</B> : "+stud1.stud_branch+"<BR/> <B>Marks</B> : "+stud1.stud_marks); document.write("<HR/>"); var stud2=new student("Abhay Patel","IT",894); student.prototype.doj=2010; document.write("<B>Name</B> : "+stud2.stud_name+"<BR/> <B>Branch</B> : "+stud2.stud_branch+"<BR/> <B>Year of Admission</B> : "+stud2.doj+"<BR/><B>Marks</B> : "+stud2.stud_marks); </SCRIPT> </BODY> </HTML>
Here is the output produced by the above JavaScript String object example program.

Here is the live demo output of the above String object example code in JavaScript.
Here is another example also demonstrates String object in JavaScript.
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>Different String Functions</TITLE> </HEAD> <BODY> <SCRIPT type="text/javascript"> var string_object_var="JavaScript String Object Tutorial at codescracker.com"; document.write("String : "+string_object_var+"<HR/>"); document.write("Length of String : "+string_object_var.length+"<HR/>"); document.write("String in Bold Text : "+string_object_var.bold()+"<HR/>"); document.write("String in Uppercase Text : "+string_object_var.toUpperCase()+"<HR/>"); document.write("String in Lowercase Text : "+string_object_var.toLowerCase()+"<HR/>"); document.write("String in Big Text : "+string_object_var.big()+"<HR/>"); document.write("String in Small Text : "+string_object_var.small()+"<HR/>"); document.write("String in Strike Text : "+string_object_var.strike()+"<HR/>"); document.write("String in Splitted Text : "+string_object_var.split('String') + "<HR/>") document.write("Index of String : "+string_object_var.indexOf('String') + "<HR/>") document.write("Substring after 10 Characters : "+string_object_var.substring(10) + "<HR/>") document.write("Character at 10th Position : "+string_object_var.charAt(10)) </SCRIPT> </BODY> </HTML>
Here is the output produced by the above JavaScript String Example.

Here is the live demo output of the above string example in JavaScript.
« Previous Tutorial Next Tutorial »