JavaScript Data Types — String

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

String

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store