What is a string in JavaScript?
By: Nay Win Hlaing
Tags: Javascript Tips, JavaScript, String, Javascript Development

There are two types of Data Types in JavaScript — primitives’ data type and non-primitive data types. The primitive data type is a kind of data type, which includes neither objects nor method (by MDN). String, Number, Boolean, null, undefined, Symbol are primitives data type and non-primitive data types are those which reference the object such as arrays, functions, objects.

Primitive data types are immutable and non-primitive data types are mutable. I will explain this in detail later in other posts.

JavaScript is a dynamic and loosely-typed language. A loosely-typed language is a language in which when we create a variable we don’t need to declare the variables’ data types as the other programming languages (e.g. whether a variable is a number or string). In JavaScript, when we create a variable and initialize some value to it, the Javascript will automatically define its data types. We don’t need to define a data type manually. And that’s why it’s called dynamic language.

String

A string is a primitive data type in JavaScript. A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. We usually put a string into either “-”(double quote) or ‘-’(single quote).

Like this:

"Hello world"; 'Hello world'; Now let’s create a string variable. let str = "hello world"; console.log(str); // hello world

The variable str is a string data type and its output would be hello world. We can also check the data type of variable “str”.

console.log(typeof str , str) ; // string hello world

Here the output would be string, hello world. We can use typeof to check the data type, and that’s how as you can see here the result is string followed by variable because we have checked like this- typeof str(variable) and (,)comma and then variables.

We can also use a single quote and double quote while initializing the string value.

Example:

let name1 = "My name is 'Mg Mg' "; let name2 = 'My name is "Mya Mya" '; console.log(name1) // My name is 'Mg Mg'; console.log(name2) // My name is "Mya Mya";

When we logged the first variable name1 the result would be My name is ‘Mg Mg’. As you can notice ‘Mg Mg’ is in a single quote. While initializing a string value, if want to put the quote into a double-quoted string value then we must use a single quote or if want to put the quote into a single-quoted string value then we must use a double quote.

There is another way to put a single quote or double quote in a string value.

let name1 = "My name is \" Mg Mg \" "; console.log(name1) ; //My name is "Mg Mg"

Here when we log the name1 the result is my name is “Mg Mg”. We can use backward slash(\) to put single quote(‘’) or double quote(“”) into a string value.

We can also concatenate strings using the concatenation sign “+”.

Like this:

let str = "Hello" + 'world'; console.log(str); // Hello world

Here "Hello" and 'world' become one string value and the result is Hello world. And we can also create two string values and can concatenate them into one string value:

let str1 = “Hello”; let str2 = "world"; console.log(str1 + str2) ; // Hello world

We can also use the string variable into zero based[0] character array.

let str = "Hello world"; console.log(str[0]); console.log(str.length);

Here we get the first character of string ‘H’. We use str.length to know the total length of the string value and the length is 11.