Javascript Strings

Working with strings in Javascript

Working with strings in Javascript

Strings are one of the data types of javascript which are used to hold the text data. It's the most used data type in javascript.

How to create strings?

let string1 = "A string primitive";
let string2 = 'Also a string primitive';
let string3 = `Yet another string primitive`;

Character access at a position with charAt(index)

let name = "Bala";
let letter = name.charAt(0)
console.log(letter)
// B

Character access at a position with charCodeAt(index)

All symbols handled in a number format in the computer which is ASCII code. charCodeAt method returns the ASCII number.

let name = "Bala";
let ascii_number = name.charCodeAt(0)
console.log(ascii_number)
// 66

Comparing strings in javascript

let a = 'a'
let b = 'b'
if (a < b) { // true
  console.log(a + ' is less than ' + b)
} else if (a > b) {
  console.log(a + ' is greater than ' + b)
} else {
  console.log(a + ' and ' + b + ' are equal.')
}

Find length of string

let searchEngine = "Google"
let count = searchEngine.length
console.log(count)
// 6

Finding a substring using indexOf(str) and lastIndexOf(str)

The indexOf() method returns the position of the first occurrence of a specified text in a string

let text = "I love to visit beach";
var pos = text.indexOf("beach");
console.log(pos)
// 16

The lastIndexOf() method returns the index of the last occurrence of a specified text in a string

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
console.log(pos)
// 21

Both indexOf(), and lastIndexOf() return -1 if the text is not found.

Escaping special characters

To escape special characters, you use the backslash \ character.

For example:

  • Windows line break: '\r\n'
  • Unix line break: '\n'
  • Tab: '\t'
  • Backslash '\'

The following example uses the backslash character to escape the single quote character in a string:

let str = 'I\'m a string!';
console.log(str)
// I'm a string

To concatenate two or more strings, you use the + operator:

Concatenating strings via + operator

let name = 'John';
let str = 'Hello ' + name;

console.log(str);
// "Hello John"

If you want to assemble a string piece by piece, you can use the += operator:

let className = 'btn';
className += ' btn-primary'
className += ' none';

console.log(className);
// btn btn-primary none

coverting other data types to string notation

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type. The following code illustrates this:

let number = 10;
console.log(number.toString());
// "10"

let is_valid = false;
console.log(is_valid.toString());
// "false"

const o = new Object();
console.log(o.toString());
// [object Object]

converting to lowercase using toLowerCase() method

The JavaScript String toLowerCase() method returns the given string in lowercase letters.

var s1 = "Hello World";  
var s2 = s1.toLowerCase();  
console.log(s2)
// hello world

converting to uppercase using toUpperCase() method

The JavaScript String toUpperCase() method returns the given string in uppercase letters.

var s1 = "Hello World";  
var s2 = s1.toUpperCase();  
console.log(s2)
// HELLO WORLD

get substring from string using method slice(beginIndex, endIndex)

slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive.

var s1="Get me something";  
var s2=s1.slice(7,16);
console.log(s2)
// something

String concatenation using concat(str1, str2, ...strN)

It simply concatenates the string and returns it.

let str1 = "Hello"; 
let str2 = "World.";
let str3 = "I'm Chitti";

let output = str1.concat(str2, str3)
console.log(output)
// Hello World.I'm Chitti