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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to uninstall Cocoapods from the Mac OS?MiscellaneousEffects in ReactReactCreate a galley with overlapping images using CSS gridCSSExplanation of PostgreSQL PgAdmin interfacePostgresA list of wp-cli commands to use via SSHWordPressWhat are Identifiers in JavaScript?JavaScript