String in Javascript
--String is a primitive data type in javascript unlike other languages like java
let str="Hello world";
console.log(typeof(str)); //output : string
let str1=new String("hellooooo");
console.log(typeof(str1)); //output : object
--However when we use its methods it treat itself as an object on a temporary basis in some cases that means it is using --the concept boxing or wrapping(process of converting primitive datatype to reference dt temporarily)
--Once the operation is complete, the string goes back to being a primitive value
Example:
let upper=str.toUpperCase(str);
console.log(upper); //output : HELLO WORLD
console.log(typeof(upper)); //output : string
Some Examples of String Methods:
//some other methods of string
console.log(str.toLowerCase()); //hello world
console.log(str.length); //11
console.log(str.indexOf('Hello')); //0
console.log(str.replace('Hello','Hey')); //Hey world
//op: Hello world
console.log(str.trim()); //trim whitespaces from both end of string
console.log(str.trimStart()); //trim only left end of string
console.log(str.trimEnd()); //trim only right end of string
console.log(str.includes('o')); //true
console.log(str.charAt(0)); //H
console.log(str.startsWith('Hell')); //true
console.log(str.endsWith('orld')); //true
Some Escape Character in Javascript:
op
hello Alfiya,
how are you
*/
console.log("hello Alfiya, \n how are you"); // \n represent new line
// op : hello "Alfiya" how are you
console.log("hello \"Alfiya\" how are you"); // \" is used to add double quotation in javascript string
console.log("hello Alfiya,\t how are you"); // \t represents tab //op : hello Alfiya how are you
Template Literal -- use to write the display the output of the text
as written in the code (written in ``backtick)
let demo=`
Hello Alfiya,
Thanks for writting this blog
`
console.log(demo)