Skip to content
  • Quizes
  • QStack
  • Blog
  • pinterest
  • instagram
  • twitter
  • linkedin
  • facebook
W3 Coding Schools
  • Home
  • HTML
    • HTML Introduction
    • HTML Editors
    • Fundamentals of HTML
    • Elements of HTML
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Style Attribute
    • HTML Text Formatting
    • HTML Phrase Tags
    • HTML Comments
    • HTML with CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Blocks
    • HTML Class Attribute
    • HTML Id Attribute
    • HTML Iframes
    • HTML JavaScript
    • HTML File Paths
    • HTML Head
    • Computer Code Elements
    • HTML Entities
    • HTML Charset
    • HTML URL Encode
    • HTML and XHTML
    • HTML Layouts
    • HTML Forms
    • HTML Form Elements
    • HTML Form Input Types
    • HTML Input Attributes
    • HTML5 Introduction
    • HTML5 New Elements
    • Semantic Elements
    • HTML5 Migration
    • Style Guide
    • HTML Canvas
  • CSS
    • CSS Introduction
    • CSS Syntax and CSS Comments
    • CSS Selectors
    • How to add CSS to a Webpage
    • CSS Color Basics
    • CSS Background Properties
    • CSS Border Properties
    • CSS Margin and Padding Properties
    • CSS Height and Width properties
    • CSS Box Model
    • CSS Outline Properties
    • CSS Fonts
    • CSS Text
    • How To Add Icons
    • CSS Links
    • CSS List-Style
    • CSS Tables
    • CSS Display Property
    • CSS Position Property
    • CSS Overflow Property
    • CSS max-width Property
    • CSS Float and Clear Properties
    • CSS Alignment
    • CSS inline-block
    • CSS Combinators
    • CSS Pseudo Classes
    • CSS Pseudo Elements
    • CSS Opacity
    • CSS Navigation Bar
    • CSS Dropdowns
    • CSS Image Gallery
    • CSS Image Sprites
    • CSS Attribute Selector
    • CSS Styling Forms
    • CSS Counters
    • CSS Units
    • CSS Specificity
    • CSS Website Layout
    • CSS Rounded Corners
    • CSS Border Image Property
    • CSS Multiple Backgrounds
    • CSS Gradient
    • CSS Shadow Effects
    • CSS Text Effects
    • CSS Web Fonts
    • CSS 2D Transforms
  • Bootstrap 4
    • Bootstrap 4 – Introduction
    • Bootstrap versions
    • Bootstrap 4 Layout
    • Bootstrap 3 Vs Bootstrap 4
    • Bootstrap 4 Grid System
    • Bootstrap 4 Typography
    • Bootstrap 4 Colors
    • Bootstrap 4 Images
    • Bootstrap 4 Tables
    • Bootstrap 4 Jumbotron
    • Bootstrap 4 Figures
    • Bootstrap 4 Alerts
    • Bootstrap 4 Buttons
    • Bootstrap 4 Button Group
    • Bootstrap 4 Badges
    • Bootstrap 4 Spinners
    • Bootstrap 4 Progress Bars
    • Bootstrap 4 Pagination
    • Bootstrap 4 Breadcrumbs
    • Bootstrap 4 List Groups
    • Bootstrap 4 Cards
  • jQuery
    • jQuery Introduction
    • jQuery Download
    • jQuery Selectors
    • jQuery Event Methods
    • jQuery Hide/Show Effects
    • jQuery Fading Effects
    • jQuery Sliding Effects
    • jQuery Animation
    • jQuery Stop and Callback
    • jQuery Get and Set Methods
    • jQuery Chaining
    • jQuery Add
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
  • Javascript
    • JS Introduction
    • JS Where to Put
    • JavaScript Syntax
    • JavaScript Comments
    • JavaScript Variables
    • JavaScript Data Types
    • JavaScript Operators
    • JavaScript Events
    • JavaScript Strings
    • JavaScript Numbers
  • php
    • PHP Introduction
    • Install PHP
    • PHP Syntax and Comments
    • PHP Variables
    • PHP Constants
    • PHP Echo and Print
    • PHP Data Types
    • PHP Strings
    • PHP Operators
    • PHP $ and $$ Variables
  • WordPress
    • WordPress Introduction
    • WordPress History
    • WordPress.com vs WordPress.org
    • How to Install WordPress
    • WordPress Dashboard
    • How to Create a WordPress Website
    • WordPress Posts
    • WordPress Pages
    • WordPress Posts vs Pages
    • WordPress Categories
  • SEO
    • SEO Introduction
    • SEO Tactics and Methods
    • SEO Relevant Filenames
    • SEO Domain Name
    • Website Design and SEO
    • SEO Keywords
    • Meta Tags Optimization
    • Title Tag Optimization
    • Anchor Text Optimization
    • Content Optimization
  • Android
    • Android Introduction
    • Android History and Versions
    • Android Architecture
    • Android Environment Setup
    • Android Application Components
    • Hello World Application
    • Android Activities
  • iOS
    • iOS Introduction
    • iOS Environment Setup
    • iOS Architecture

You Are Here

  • Home
  • Learn Javascript
  • JavaScript Strings

Topics

  • JavaScript Numbers
  • JavaScript Strings
  • JavaScript Events
  • JavaScript Operators
  • JavaScript Data Types
  • JavaScript Variables
  • JavaScript Comments
  • JavaScript Syntax
  • JS Where to Put
  • JS Introduction

Oct Champs & Prizes

  • 1. Pooja Ladda
  • 2. Manjali Kuldharan
  • 3. Pranali Surawar
  • 4. Anjali Kulkarni
  • 5. Vishal Deshmukh

Recent Posts

  • HTML Media
  • jQuery Siblings
  • Bootstrap 4 Cards
  • jQuery Descendants
  • jQuery Ancestors

JavaScript Strings

What is JavaScript String?

A string is a combination of letters, numbers, special characters, and arithmetic values or a combination of all. JavaScript string can be created by enclosing the string character(literal) either in single quotes (‘) or in double-quotes (“), as shown in the following example:

Example:

var myString = 'Learn JavaScript!'; // Single quoted string
var myString = "Learn JavaScript!"; // Double quoted string
Try it
You can use quotes inside a JavaScript string, only when they don’t match the quotes surrounding the string:

Example:

var str1 = "It's okay";
var str2 = 'He said "Welcome"';
var str3 = "She replied 'Thank you'";
Try it
You can use single quotes inside single-quoted strings or double quotes inside double-quoted JavaScript strings by escaping the quotes with a backslash character (\), like this:

Example:

var str1 = "It\'s okay";
var str2 = "He said \"Welcome\"";
var str3 = 'She replied \'Thank you\'';
Try it
The backslash (\) is called an escape character and the sequences \’ and \” that we’ve used in the above example are called escape sequences.

Escape Sequences of JavaScript:

Escape sequences are useful in situations when you want to use characters that can’t be typed using a keyboard. The following are some most commonly used escape sequences.
  • \n is replaced by the newline character
  • \t is replaced by the tab character
  • \r is replaced by the carriage-return character
  • \b is replaced by the backspace character
  • \\ is replaced by a single backslash (\)

Performing Operations on Strings:

JavaScript provides some properties and methods to perform operations on string values. Following are some string properties and methods:

Finding the lenght of a string:

The JavaScript length property returns the length of the string, which is the number of characters contained in the string. It includes the number of special characters as well, such as \t or \n.

Example:

var str = "JavaScript is a client-side scripting language and it is used to enhance the interaction of the user with the webpage.";
document.getElementById("trial").innerHTML = str.length;
Try it
Note: The length is a property, not a function, so don’t use parentheses after it like str.length(). Instead of this just write str.length, otherwise, it will produce an error.

Finding a String Inside Another String:

The indexOf() method is used to find a substring i.e string within another string. The indexOf() method returns the index or the first occurrence of a specified string inside the string.

Example:

var str = "Its okay not to be 'okay!'";
var position = str.indexOf("okay");
document.getElementById("trial").innerHTML = position;
Try it
Similarly, you can use the lastIndexOf() method to get the index or position of the last occurrence of the specified string within a string, like this:

Example:

var str = "Its okay not to be 'okay!'";
var position = str.lastIndexOf("okay");
document.getElementById("trial").innerHTML = position;
Try it

Searching for a pattern inside the string:

The search() method is used to search a particular piece of text or a pattern inside a string. It returns the index of the first match like indexOf() method and returns -1 if no matches were found, but unlike indexOf() method this method can also take a regular expression as an argument to provide advanced search capabilities.

Example:

var str = "Its okay not to be 'okay' occurs!";
var position = str.search("okay");
Try it

Extracting a Substring from a String:

The slice() method is used to extract a part or substring from a string. The slice() method takes 2 parameters: start index (index at which to begin extraction), and an optional end index (index before which to end extraction), like str.slice(startIndex, endIndex).
The following example slices out a portion of a string from position 10 to position 21:

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.slice(10,21);
Try it

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.slice(10,21);
Try it
You can also specify negative values to extract the substring from the string. If the negative value is specified then the position is counted from the end of the string.

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.slice(-18,-8);
Try it
If the second parameter is not specified then the method will slice out the rest of the string.

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.slice(10);
Try it

Extracting a Substring from a String using substring() Method:

The substring() method is used to extract a part or substring from a string and it is similar to the slice() method. The only difference is that the substring() method cannot accept negative indexes.

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.substring(10,21);
Try it

Extracting a Fixed Number of Characters from a String:

The substr() method provided by JavaScript is similar to the slice() method with a subtle difference, the second parameter specifies the number of characters to extract instead of ending index, like str.substr(startIndex, length).

Example:

var str = "HTML, CSS, JavaScript, jQuery";
var result = str.substr(10,11);
Try it

Replacing the Contents of a String:

The replace() method is used to replace part of a string with another string. It takes two parameters a regular expression to match a substring to be replaced and a replacement string, like str.replace(regexp|substr, newSubstr). It return a new string, it doesn’t affect the original string that will remain unchanged.

Example:

var str = document.getElementById("trial").innerHTML;
var txt = str.replace("client-side scripting language","used to enhance the interaction of the user with the webpage");
Try it
Note: By default, the replace() method replaces only the first match, and it is case-sensitive.

Converting a String to Uppercase or Lowercase:

If you want to convert string to uppercase then you can use toUpperCase() method.

Example:

var text = document.getElementById("trial").innerHTML;
document.getElementById("trial").innerHTML = text.toUpperCase();
Try it
Similarly, if you want to convert string to lowercase then you can use toLowerCase() method.

Example:

Var text = document.getElementById("trial").innerHTML;
document.getElementById("trial").innerHTML = text.toLowerCase();
Try it

Concatenating Two or More Strings:

You can combine two or more strings using the + and += assignment operators.

Example:

var text1 = "JavaScript is a";
var text2 = "client-side scripting language.";
var text3 = text1.concat(" ",text2);
document.getElementById("trial").innerHTML = text3;
Try it
Note: JavaScript also provides concat() method to combine two strings, but it is not recommended.

Accessing Individual Characters from a String:

The charAt() method is used to access individual characters from a string, like str.charAt(index). The index specified should be an integer between 0 and str.length – 1. If no index is provided the first character in the string is returned, since the default is 0.

Example:

var str = "JavaScript";
document.getElementById("trial").innerHTML = str.charAt(0);
Try it

Splitting a String into an Array:

The split() method is used to split a string into an array of strings.
The syntax of split() method is str.split(separator, limit).
The separator argument specifies the string at which each split should occur and the limit arguments specify the maximum length of the array.
If the separator argument is not found or omitted in the specified string then the entire string is assigned to the first element of the array.

Example:

var str = "HTML,CSS,JavaScript,jQuery";
var arr = str.split(",");
document.getElementById("trial").innerHTML = arr[2];
Try it

Post navigation

JavaScript Events
JavaScript Numbers

Ask a Question Cancel reply

Your email address will not be published. Required fields are marked *

W3 CODING SCHOOLS © Copyright 2019-20
Privacy policy   Terms of use

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok