YogeshChauhan.com
JavaScript String Properties and Methods
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

search()    

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

substring()    

Extracts the characters from a string, between two specified indices


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

//ell

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!
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS Mar 2 WordPress: How to access first and random row values from a repeater field in ACF? Mar 1 The basics of CSS Box model Feb 27 WordPress: How to display slider repeater fields in ACF? Feb 26 Simple Page Hit Counter in PHP

You might also like these

2 Ways We Can Write Multiple Line Commands in PHPPHPbin2hex() and chr() String Functions in PHPPHPNew to ┬áDrupal? Here’s What You Need to KnowDrupalHow to retrieve homepage URL in WordPress?WordPressFull and Partial CUBE in Postgres with ExamplesPostgresHow to import MySQL small sample database into phpMySQL?SQL/MySQL