What are Strings in JavaScript and how to initialize them?

What are Strings in JavaScript and how to initialize them?

1 Like

Hi Yanisleidi,

Strings are specialized data types for storing and manipulating sequences of Unicode characters. In JavaScript, data types are classified into three main categories:

  1. Primitive Data Types
  2. Non-Primitive (Composite) Data Types
  3. Trivial or Special Data Types

Primitive data types are elementary to a programming language. These are low level user-defined variables that can hold only one value at a time. Numbers, Strings, and Booleans come under primitive data types in JavaScript.

Composite data types are built from primitive data types. They are commonly found in programming and include objects and arrays.

Trivial data types include null and undefined. Each of these defines only a single value.

There are two ways to initialize a string in JavaScript:

  1. By string literal
  2. By string object using “new” keyword

By string literal

This method enables us to declare primitive strings. Here, we directly assign the value of the string to the variable.

var message= “Hello”; // Double quotes
var message= ‘Hello’; // Single quotes

By string object using “new” keyword

The new keyword creates an instance of the String. Here, we pass the string value as a parameter to the constructor of the “String” class.

var message= new String (“Hello”); // Double quotes
var message= new String (‘Hello’); // Single quote

In order to deep-dive into JavaScript Strings In-built Properties and Methods and see how you can use JavaScript Strings with Selenium WebDriver, please go through the following blog:

3 Likes