Yogesh Chauhan's Blog

JavaScript String Properties and Methods

in JavaScript on June 20, 2020

JavaScript String Properties

constructor

Returns the string’s constructor function


var str = "Hello World!";
console.log(str.constructor);

//function String() { [native code] }

length

Returns the length of a string


var str = "Hello World!";
console.log(str.length);

//12

prototype    

Allows you to add properties and methods to an object


function employee(name, jobtitle, born) {
  this.name = name;
  this.jobtitle = jobtitle;
  this.born = born;
}
employee.prototype.salary = 2000;

var fred = new employee("Fred Flintstone", "Caveman", 1970);
document.getElementById("demo").innerHTML = fred.salary;

//2000

String Methods

charAt()

Returns the character at the specified index (position)


var str = "HELLO WORLD";
console.log(str.charAt(0));

//H

charCodeAt()

Returns the Unicode of the character at the specified index


var str = "HELLO WORLD";
console.log(str.charAt(0));

//72

concat()

Joins two or more strings, and returns a new joined strings


var str1 = "Hello ";
var str2 = "world!";
console.log(str1.concat(str2));

//Hello world!

endsWith()

Checks whether a string ends with specified string/characters


var str = "Welcome to the world.";
console.log(str.endsWith("world."));

//true

fromCharCode()

Converts Unicode values to characters


console.log(String.fromCharCode(65));

//A

includes()

Checks whether a string contains the specified string/characters


var str = "Welcome to the world.";
console.log(str.includes("world."));

//true

indexOf()

Returns the position of the first found occurrence of a specified value in a string


var str = "Hello world, welcome to the universe.";
console.log(str.indexOf("welcome"));

//13

lastIndexOf()

Returns the position of the last found occurrence of a specified value in a string


var str = "Hello planet earth, you are a great planet.";
console.log(str.lastIndexOf("planet"));

//36

localeCompare()

Compares two strings in the current locale

Returns -1 if str1 is sorted before str2, returns 0 if the two strings are equal, returns 1 if str1 is sorted after str2


var str1 = "ab";
var str2 = "cd";
console.log(str1.localeCompare(str2));

//-1

match()

Searches a string for a match against a regular expression, and returns the matches.


var str = "The rain in SPAIN stays mainly in the plain"; 
console.log(str.match(/ain/g));

//ain,ain,ain

repeat()

Returns a new string with a specified number of copies of an existing string


var str = "Hello!";

console.log(str.repeat(2));

//Hello!Hello!

replace()

Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced


var str = "hello"; 
var str = str.replace("hello", "hello world");
console.log(str);

//hello world

Searches a string for a specified value, or regular expression, and returns the position of the match


var str = "Visit yogeshchauhan.com!"; 
console.log(str.search(" yogeshchauhan.com"));

//6

slice()    

Extracts a part of a string and returns a new string


var str = "Hello world!"; 
console.log(str.slice(0, 5));

//Hello

split()

Splits a string into an array of substrings


var str = "How are you doing today?";
console.log(str.split(" "));

//How,are,you,doing,today?

startsWith()

Checks whether a string begins with specified characters


var str = "Welcome to the world.";
console.log(str.startsWith("Welcome"));

//true

substr()

Extracts the characters from a string, beginning at a specified start position, and through the specified number of character


var str = "Hello world!";
console.log(str.substr(1, 4));

//ello

substr method: few more examples

substring()

Extracts the characters from a string, between two specified indices


var str = "Hello world!";
console.log(str.substring(1, 4));

//ell

Checkout this post for more examples on substring method: substring() Method in JavaScript

The difference between substring() and substr()

toLocaleLowerCase()

Converts a string to lowercase letters, according to the host’s locale


var str = "Hello World!";
console.log(str.toLocaleLowerCase());

//hello world!

toLocaleUpperCase()

Converts a string to uppercase letters, according to the host’s locale


var str = "Hello World!";
console.log(str.toLocaleUpperCase());

//HELLO WORLD!

toLowerCase()

Converts a string to lowercase letters


var str = "Hello World!";
console.log(str.toLowerCase());

//hello world!

toString()

Returns the value of a String object


var str = "Hello World!";
console.log(str.toString());

//Hello World!

toUpperCase()

Converts a string to uppercase letters


var str = "Hello World!";
console.log(str.toUpperCase());

//HELLO WORLD!

trim()

Removes whitespace from both ends of a string


var str = "     Hello World!     ";
console.log((str.trim());

//Hello World!

valueOf()

Returns the primitive value of a String object


var str = "Hello World!";
console.log(str.valueOf());

//Hello World!

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to remove special characters (dash, asterisk etc) from any string in PHP?PHPHow to disable scrolling on html body on menu click using JavaScript?JavaScriptQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLWhat is Hadoop and Hadoop Ecosystem?MiscellaneousAdvanced Array Methods in JavaScript (with examples)JavaScriptHow to create Flickering Texts using CSS?CSS