The JavaScript string is an object that represents a sequence of characters.
There are 2 ways to create strings in JavaScript
- By string literal
- By string object (using new keyword)
1) By string literal
The string literal is created using double quotes. The syntax of creating a string using string literal is given below
Var str= “This is a string”;
const str="This is a string";
console.log(str);
// Output : "This is a string"
2) By string object (using new keyword)
The syntax of creating a string object using a new keyword is given below
Syntax:
Var str= new String(“This is a string”);
Here new keyword is used for creating string
var str=new String("hello how are you");
console.log(str);
// Output : String { "hello how are you" }
There are various string methods available in javascript
i) charAt() method
It provides the char value present at the specified index.
var str="javascript";
console.log(str.charAt(5));
// Output : "c"
ii) concat() method
It provides a combination of two or more strings.
var a="javascript ";
var b="is very popular";
var c=a.concat(b);
console.log(c);
// Output : "javascript is very popular"
iii) toLowerCase()
It converts the given string into a lowercase letter
var a="JavaScript Is Popular";
var b=a.toLowerCase();
console.log(b);
// Output :"javascript is popular"
iv) toUpperCase()
It returns the given string in uppercase letters.
var a="JavaScript Is Popular";
var b=a.toUpperCase();
console.log(b);
// Output :"JAVASCRIPT IS POPULAR"