Javascript Basic – What is String in Javascript

Javascript String

JavaScript string object is used for storing and manipulating a sequence of characters. Strings are useful for storing and managing text. Some of the operations on strings are to check their length, to concatenate them using + and +=, to check the existence or location of substrings with the index of the ( ) method, or to extract substrings with the substring ( ) method.

Strings are a fundamental data type in JavaScript and are used extensively in web development. Strings in JavaScript are sequences of characters, such as letters, numbers, and symbols, enclosed in quotes (single or double).

One of the most important uses of strings in JavaScript is in displaying text on a web page. For example, to display a message on a webpage, you can use the document.write() method to output a string to the page. You can also use strings to display text in input fields or as labels for form elements.

Strings can also be manipulated in JavaScript using a variety of methods. For example, you can concatenate strings using the + operator or the concat() method. You can also search for a particular substring within a string using the indexOf() method or replace a substring with another string using the replace() method.


A JavaScript string is zero or more characters written inside quotes whether single or double it is. (String Literal)

Example:

var string_1 = 'Hello !';
var string_2 = "Hello !";

You can declare a string by this string constructor.

var str = String('Hello !');
>>> Hello !

Escape Notation

When you want to quote any or some words inside a string quotation then you have to use escape notation ( \ )

NotationResult
\’  \’Quote any or some words inside a string quotation
\nNew line
\tTab
\\For using a backslash

String Comparison (Lexicographic Order)

Lexicographical order essentially means “dictionary order”. In simpler terms, a < b if the first character of a comes before the first character of b in the ASCII table, or if the first characters of a and b are equal.

Example:

'alpha' < 'alpaq'; // false, ‘h’ is greater than ‘a’
'alpha' > 'Alpha'; // true, ‘a’ is greater than ‘A’
'alpha' == 'ALPHA'; // false, ‘a’ is greater than ‘A’
 
['zeta', 'Zeta', 'ZETA', 1, '1', ''].sort(); // [ '', 1, '1', 'ZETA', 'Zeta', 'zeta' ]

String Method

For many string operations, there are some handy methods in Javascript also known as the Javascript string method. Here are some examples:-

Example:

var a = 'I am';
var b = 'Mahdi';
var c = a.concat(' ', b);
	>>> I am Mahdi
 
(variable Name).substr(from where to start, how much character);
c.substr(5, 4);
>>> Mahd
c.charAt(6); //showing character at position 6.
>>> a
a.toUpperCase(); // will convert every character to upper Case
>>> I AM
c.toLowerCase(); // will convert every character to lower Case
>>> i am mahdi
'   Mahdi  '.trim(); // remove every unwanted space character from start and end.
>>> Mahdi
'My name is Mahdi'.split('by which character'); // slit this string by that character.
>>> ‘My’, ‘name’, ‘is’, ‘Mahdi’

startsWith

This method is used to see if your desired string starts with a specific character or character set. It always returns a Boolean value true or false:

Example:

var name = 'My name is Mahdi';
console.log(name.startsWith('name'));

Output:

false

endsWith

This method is used to see if your desired string ends with a specific character or character set. It always returns a Boolean value true or false:

Example:

var name = 'My name is Mahdi';
console.log(name.endsWith('Mahdi'));

Output:

true

includes

This method is used to see if your desired string has a specific character or character set. It always returns a Boolean value true or false:

Example:

var name = 'My name is Mahdi';
console.log(name.includes('Thomas'));

Output:

false

repeat

This method is used to repeat a string multiple times:

Example:

var rpt = 'Hello World ';
console.log(rpt.repeat(4));

Output:

Hello World Hello World Hello World Hello World

Length of a String

In Javascript to find out the total number of letters of a string, there is a property named (.length).

Example:

var ln = 'Hello World';
console.log(Ln.length);

Output:

11

Strings are also important for working with APIs and parsing data from external sources, such as JSON data. Many APIs return data in the form of strings, and you may need to manipulate the string data to extract the information you need.

In addition, strings can be used to represent complex data structures, such as arrays and objects, in a compact and easy-to-read format using JSON notation. This makes it easy to exchange data between web applications and APIs.

Strings are a crucial part of web development in JavaScript. By understanding how to work with strings, you can create dynamic and interactive web applications that provide a rich user experience.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.