Data Types in JavaScript : String

String is a any form of text within double or single quotes in JavaScript. String can be primitive or object types. Every primitive data types in JavaScript have their own object type.

Ok, so what is the difference between primitive & object data types?

When we simply want to store (set) and retrieve (get) value we use primitive type string. It starts with small s. But, our goal is not always to store and display value. We perform some actions on value/data. So to use properties and methods of string it needs to convert into object type (String) with capital S.

So whenever you use properties and methods with primitive data types, JavaScript engine automatically converts primitive to object internally and let you use its functions. You don’t need to explicitly declare object type to perform actions on string. JavaScript will take care of that.

Let’s begin with string data type. We’ll be looking into string and it’s basic functions.

Declaring String

let name = 'Tuorial Funda'
let name2 = "Tutorial Funda"
let name3 = `Tutorial Funda`

In the above example we have used let keyword to declare string. We have declared string in 3 ways (Single quote, Double quote & Template literals). There is no difference between Single and Double quote.

Note: Template literals also called as Template strings. We use backtick (“) to work with it.

With Template literals you have additional advantage of using any JavaScript expression inside string. For example,

const greet= "Welcome to Tutorial Funda."
let str =`Hello, ${greet}`

In the above code, we have declared a greet variable with some text. At the next line, we have declared str variable with some text using Template literals. In the Template literals, we have used that greet variable. So inside it, using ${} you can write any variable, JavaScript expression or call a function.

Output of above code will be “Hello, Welcome to Tutorial Funda.“.

Also, with Template literals you can easily use multiline text.

Escape Character in String

What if you want to use double or single quotes inside string text? You can use Backslash Escape character (\) to accomplish that. It turns special character into strings. Let’s see the example.

const greet= "Welcome to "Tutorial Funda"."
const greet1= "Welcome to \"Tutorial Funda\"."
const greet2= `Welcome to "Tutorial Funda".`

In the above code, we are trying to highlight part of text using double quotes inside string.

Variable greet will throw an error because it will not accept double quotes inside double quotes.

In variable greet1 we have solved that problem by using Backslash Escape character (\) before each quote. It automatically converts quote into string.

In variable greet2 we have used Template strings. You can easily use double or single quotes inside it. It doesn’t need any Backslash Escape character (\).

String Manipulation

We always want to perform some action on data. String variable contains data. Let’s see how to manipulate data/value in string.

Note: There are large number of functions that we can use to manipulate string. We are not going to look into all of them.

1. Concatenation

Generally, there are two ways to concatenate string.

Using + Sign

let message = '123';
console.log(message + 2);

Output of above code will be 1232.

Using concat Function

let message = '123';

Use of concat function is the best way for string concatenation. You just need to call concat function of string variable & pass value or variable to it.

Output of above code will be 1232.

2. Converting Number to String

let num = 123;
num = num.toString();

You can convert number to string using tostring() function. In the above code, we have declared num variable which is a number. In the next line itself we are logging typeof num variable. In the output, you will get number.

On the third line, we are converting number to string using tostring() function & storing back to num variable. In the last line we have again logged the typeof num variable. You will get output string.

3. Upper & Lower Case

Sometimes you want to convert string to upper or lower case. String object has functions to convert the case you want. Let’s see the example below.

//Upper Case Example

let greet = "Web Development";
greet = greet.toUpperCase();

//Lower Case Example

let fruit = "APPLE";
fruit = fruit.toLowerCase();

Function toUpperCase() we use to convert string in uppercase and function toLowerCase() we use to convert string in lowercase.

WEB DEVELOPMENT” & “apple” output you will get for upper & lower functions respectively.

There is long list of functions/methods for string you can use to manipulate.

You have now basic knowledge about string, so you can start experiment with it.😃

Categories js

Subscribe Now!

Subscribe Us For Latest Articles