JavaScript Data Types — String

Abdullah Mamun
2 min readMay 5, 2021

--

There are many data types in JavaScript. string, number, object and more.

String

String is a sequence of characters. string must be inside single quote or double quote. if you write any number in single or double quote, it will be string also.

Creating String

let string1 = ‘This is my first string’;

let string2 = new String(‘This is my first string using string constructor’)

Character Access (charAt)

There are two way to access any character in string.

‘hello’.charAt(2) // l

let name = ‘karim’

name[1] // a

concatenates (concat)

The concat() method concatenates the multiple string and return a new string

let str1 = ‘hello’

let str2 = ‘world’

let concatStr = concat(str1, str2)

console.log(concatStr) // hello world

Includes (includes())

The includes() method performs a case sensitive search any string in another string. if string is found return, otherwise it’s return false.

let str = ‘hello world’

str.includes(‘world’) // true

str.includes(‘Hello’) // false

endsWith

endsWith() method performs a exact last character of the string.

let str = ‘hello world’;

str.endsWith(‘world’) //true

str.endsWith(‘hello’) //false

indexOf

indexOf() method return index of specific character or string. It will return first occurrence of the given value.

let str = ‘hello world’

str.indexOf(‘w’) // 6

lastIndexOf

lastIndexOf() method return index of specific character or string. It will return last occurrence of the given value.

let str = ‘hello world. The world needs you.’

str.indexOf(‘world’) // 17

Replace

replace() method returns a new string with replaced value.

let str = ‘hello world’

str.replace(‘world’, ‘bangladesh’) // hello bangladesh

Slice

slice() method extract part of string and returns a new string.

let str = ‘hello world’

str.slice(2, 7) // llo w

toUppserCase

toUpperCase() converted the value in UpperCase.

let str = ‘hello world’

str.toUpperCase() // HELLO WORLD

toLowerCase

toLowerCase() converted the value in Lower Case.

let str = ‘HELLO WORLD’

str.toLowerCase() // hello world

--

--