Loading Posts...

Javascript String Tutorial With Example | Strings in Javascript

Javascript String Tutorial With Example | Strings in Javascript

Javascript String Tutorial With Example | Strings in Javascript is today’s topic. The String global object is the constructor for strings or the sequence of characters. JavaScript strings are used for storing and modifying the text. The JavaScript string is the object that represents the sequence of characters. It wraps the Javascript’s string primitive data type with the several helper methods. As JavaScript automatically converts between the string primitives and the String objects, you can call any of the helper methods of a String object on the string primitive.

Javascript String Tutorial With Example

Strings are useful for holding the data that can be represented in the text form. Some of the most-used operations on strings are to check string length, to build and concatenate them using a + and += string operators, checking for the existence or location of the substrings with an indexOf method, or extracting the substrings with the substring() method.

There are two ways to create the string in JavaScript.

  1. By string literal
  2. By string object (using new keyword)

#By string literal

The string literal is created using the double quotes. The syntax of creating the string using string literal is given below.

var str = 'appdividend'; 
const str = 'appdividend'; 
let str = 'appdividend';

You can use quotes inside the string, as long as they don’t match the quotes surrounding a string.

// app.js

let strA = "Hello World";
let strB = "He has called 'Krunal'";
let strC = 'He has called "Krunal"';

console.log(strA);
console.log(strB);
console.log(strC);

See the output.

By string literal

 

#By string object (using new keyword)

The syntax of creating a string object using the new keyword is given below.

let data = new String('AppDividend');
console.log(data);

See the output.

Javascript String Tutorial With Example

 

 

#Find String Length in Javascript

If you want to find the length of a string, use the built-in length property.

// app.js

let strA = "Hello World";

console.log(strA.length);

See the output.

Find String Length in Javascript

 

#Special Characters in Javascript

Let’s see the following code.

let strA = "Hello World, I am "Krunal"";

Because strings must be written within quotes,  In the above code, JavaScript will misunderstand this string.

The string will be chopped to “Hello World, I am”.

The solution to avoid that problem is to use a backslash escape character.

The backslash () escape character turns the special characters into string the characters.

Code Result Description
Single quote
Double quote
\ Backslash

 

The sequence   inserts the double quote in a string.

Now, see the following code.

// app.js

let strA = "Hello World, I am "Krunal", I am a Good Boii";
console.log(strA);

See the output.

Special Characters in Javascript

 

The sequence   inserts the single quote in a string.

let x = 'It's good.';

The sequence inserts the backslash in a string.

let x = "The character \ is called backslash.";

Six other escape characters are valid in the JavaScript.

Code Result
b Backspace
f Form Feed
n New Line
r Carriage Return
t Horizontal Tabulator
v Vertical Tabulator

#Strings Can be Objects

Normally, the js strings are primitive values, created from the literals:

let nameA = "Ankit";

But the strings can also be defined as objects with the keyword new.

let nameB = new String("Ankit");

Now, let’s use a typeof operator to check the variables’ type.

// app.js

let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(typeof(nameA))
console.log(typeof(nameB))

See the output.

Strings Can be Objects
When using the == operator, equal strings are equal.
// app.js

let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(nameA == nameB)

See the output.

Double equal operator in Javascript

 

When using the === operator, equal strings are not equal, because the === operator expects equality in both type and value.

Let’s check the above example with the === operator.

// app.js

let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(nameA === nameB)

See the output.

Triple equal operator in Javascript

 

#String Properties in Javascript

Here is a list of the properties of String obj and their description.

Sr.No. Property & Description
1 constructor

Returns the reference to the String function that created the object.

2 length

Returns a length of the string.

3 prototype

The prototype property allows you to add the properties and methods to an object.

Long literal strings in Javascript

Sometimes, your code will include the strings which are very long. Rather than having lines that go on endlessly, or wrap at a whim of your editor, you may wish to specifically break a string into multiple lines in a source code without affecting an actual string contents. There are following two ways you can do this.

You can use a + operator to append multiple strings together, like the following code.

let longString = "This is a very long string which needs " +
                 "to wrap across the multiple lines because " +
                 "otherwise my code is unreadable and messy";

Or you can use a backslash character (“”) at the end of each line to indicate that a string will continue on the next line. Make sure that there is no space or any other character after a backslash (except for a line break), or as the indent; otherwise it will not work. That form looks like the following code.

let longString = "This is a very long string which needs 
to wrap across the multiple lines because 
otherwise my code is unreadable and messy.";

#JavaScript String Methods

Let’s see a list of JavaScript string methods.

Methods Description
charAt() It provides a char value present at the specified index.
charCodeAt() It provides a Unicode value of a character present at the specified index.
concat() It provides the combination of two or more strings.
indexOf() It provides a position of a char value present in the given string.
lastIndexOf() It provides a position of a char value present in the given string by searching the character from the last position.
search() It searches the specified regular expression in a given string and returns its position if a match occurs.
match() It searches the specified regular expression in a given string and returns that regular expression if the match occurs.
replace() It replaces the given string with the specified replacement.
substring() It is used to fetch the part of the given string on the basis of the specified index.
slice() It is used to fetch the part of the given string. It allows us to assign positive as well negative index.
toLowerCase() It converts a given string into the lowercase letter.
toLocaleLowerCase() It converts a given string into lowercase letter on the basis of the host?s current locale.
toUpperCase() It converts a given string into an uppercase letter.
toLocaleUpperCase() It converts a given string into uppercase letter on the basis of a host’s current locale.
toString() It provides a string representing the particular object.
valueOf() It provides the primitive value of the string object.

 

Finally, Javascript String Tutorial With Example | Strings in Javascript post is over.

The post Javascript String Tutorial With Example | Strings in Javascript appeared first on AppDividend.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

user

The author didnt add any Information to his profile yet

Loading Posts...