String in JavaScript- Complete guide

String in JavaScript- Complete guide

Hey readers, in this article, we will be covering all about String objects and how to use them in JavaScript. Before jumping directly to the Strings, we will learn about JavaScript and the basics of what exactly is String. So if you are new to all these concepts, don’t worry, we will be covering it right from the fundamentals and will then move to differences. So keep reading.

Originally, the JavaScript language was designed solely for use in browsers. However, the language is being utilized in a variety of other settings. It’s a scripting language that allows you to generate and manage dynamic website content. It means that the webpage can move, refresh, or alter on the user’s screen without requiring a manual reload.

What are Strings in JavaScript?

Strings are strings that serve to hold data that can be represented in Javascript.

A JavaScript string, such as “Codedamn,” stores a series of characters. Any text enclosed in double or single quotations can be considered a string. As an example,

let code= "codedamn";Code language: JavaScript (javascript)

String indexes begin at 0. The first character appears in place 0, the second in position 1, and so on. Because JavaScript immediately translates from string primitives to objects, we can call any of the pre-defined methods.

Methods

method charAt(indexOfCharacter): The character at the supplied index is returned by this procedure. In JavaScript, indexing is done on a zero-based basis.

Parameters: This method takes only one parameter, indexOfCharacter, which is the index of any string’s character.

This example shows how to use the JavaScript string charAt() technique.


let code =’Codedamn’;
let damn= ‘Codedamn is the best platform to "
 + "learn and experience development.';

// Print the string as it is
document.write(code);
document.write("<br>");
	
document.write(damn);
document.write("<br>");

// As string index starts from zero
// It will return first character of string
document.write(code.charAt(0));
document.write("<br>");
	
document.write(damn.charAt(5));Code language: JavaScript (javascript)

The method charCodeAt(indexOfCharacter) returns an integer that indicates the Unicode value of the character at the supplied index. Only one parameter is accepted by this procedure.

concat( objectOfString ) Method: It concatenates two strings and returns combined output.

let code= 'Codedamn';
let damn= 'Hello';

// Accessing concat method on an object
// of String passing another object
// as a parameter
document.write(code.concat(damn));Code language: JavaScript (javascript)

endsWith(queryString, length) Method: This function determines whether a string contains the supplied string of characters at the end. If the string ends with the specified string, this method returns true otherwise it returns false. It’s important to note that this approach is case-sensitive. There are two arguments that can be passed to this procedure.

queryString: The string that will be looked up.

length: The length of the string you provided is the default value.

This example shows how to use the JavaScript String endsWith() method.

let code= 'Codedamn';
let damn= 'Hello';
document.write(damn.endsWith('lo'));Code language: JavaScript (javascript)

fromCharCode(UNICODE_NUMBER) Method: UNICODE values are converted to characters using this method. This is a String object’s static method. The first method does not use the string variable as a starting point. This function returns the character associated with the supplied UNICODE. This method takes only one parameter, UNICODE-NUMBER, which is the number for which you want to generate a character.

This example shows how to use the JavaScript String fromCharCode() method.

let code= ‘Codedamn';
let damn= 'Hello';
document.write(String.fromCharCode(104));Code language: JavaScript (javascript)

toLowerCase(stringVariable) Method: This method transforms all of the characters in a string to lowercase and provides a new string containing only lowercase characters. This function takes only one parameter: stringVariable string to change to lowercase.

This example shows how to use the JavaScript String toLowerCase() method.

let code= ‘Codedamn';
let damn= 'Hello';

document.write(code.toLowerCase(code));Code language: JavaScript (javascript)

toUpperCase(stringVariable) Method: This function changes all of the characters in a string to upper case and returns a new String containing only upper case characters. This function takes only one parameter: stringVariable string to change to upper case.

This example shows how to use the JavaScript String toUpperCase() method.

let code= ‘Codedamn';
let damn= 'Hello';

document.write(code.toUpperCase(code));Code language: JavaScript (javascript)

trim() Method: This method is used to remove either white spaces or punctuation marks from a string. This method creates a new string with the white spaces deleted. A String object was used to call this method. There are no parameters accepted by this procedure.

Example: The trim() method of JavaScript String is demonstrated in this example.

let code= ‘Codedamn';
let damn= 'Hello';

// Storing new object of string
// with removed white spaces
var newCode =code.trim();

// Old length
document.write(code.length);
document.write("<br>");

// New length
document.write(newCode.length)
Code language: JavaScript (javascript)

Conclusion

This was all about the String objects in JavaScript. If you have any query related to React or JavaScript, do drop it down in the comment section also do check out codedamn courses if you want to learn more about JavaScript and React with its use cases and amazing projects. They also have an in-built playground for different programming languages and environment sets so do check that out and join codedamn’s community

Hope you like it.

Sharing is caring

Did you like what mansi wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far