Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

JavaScript String is a primitive data type and is used for storing and manipulating a sequence of characters. It can contain zero or more characters within quotes single or double.

We will see various ways to work with string in JavaScript. Creating a string in JavaScript is similar to other languages we just have to put the string data in between single quotes(‘ ‘)/double quotes(” “). Another way is to pass the string in the String Constructor.

Syntax:

let string_name=".."

Below examples illustrate the JavaScript String:

Example: In this example we will create a string in a native way and by using a constructor as well.

javascript




// Creating a string by assigning
let Str_val = "Welcome Geeks"
console.log(Str_val);
 
// Creating a string using String() constructor
let Str1_val = String(5)
console.log(typeof Str1_val+ ":" + Str1_val)


Output: If we used String() constructor then a number can be a string as well.

Welcome Geeks
string:5

Example: Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string.

javascript




let x = "'GeeksforGeeks'";
let y = "A 'computer' 'science' portal";
 
console.log(x + " is " + y);


Output:

'GeeksforGeeks' is A 'computer' 'science' portal

String Comparison: There are some inbuilt methods with that, we can compare strings such as equality operator and another method localeCompare().

Example: In this example, we will use the above methods to compare strings.

Javascript




function Str_func() {
    let Str_val = "John"
    let Str_value = String("John");
    console.log(Str_val==Str_value);
    console.log(Str_val.localeCompare(Str_value));
     
}
Str_func();


Output: The Equality operator returns true, whereas the localeCompare method returns the difference of ASCII values.

true
0

Example: Strings can be used as objects by using the keyword ‘new‘.

javascript




// Declare a string
let x = "Great Geek";   
 
// Declare an object
let y = new String("Great Geek");
 
console.log(typeof x + "-" + typeof y);


Output:

string-object

Example: We will convert other primitive data types to String.

Javascript




function Str_func() {
    let Str_val = Number(90)
    let Str_value = Boolean(true);
    console.log(String(Str_val));
    console.log(String(Str_value));
     
}
Str_func();


Output:

90
true

Example: We will implement string methods such as indexOf, slice, replace, and toLowerCase.

Javascript




let x = "GeeksforGeeks";
console.log(x.indexOf("Geeks"))
console.log(x.slice(0,5));
console.log(x.replace("Geek", "Super Geek"));
console.log(x.toLowerCase());


Output:

0
Geeks
Super GeeksforGeeks
geeksforgeeks

We have an article where we have shown example code of the most useful methods and properties, to know more about the string please check String Methods article.

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

We have a complete list of Javascript string methods, to check those please go through this Javascript String Reference article.


My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials