moreshwar

Moreshwar Pidadi

Posted on March 5, 2022

Strings in JS

The string object is used to represent and manipulate the character.

Note: that JavaScript distinguishes between String objects and primitive String values.


  1. String literals (denoted by double ("Moreshwar") or single ('Moreshwar') quotes).

  2. Strings returned from String calls in a non-constructor context (that is, called without using the new keyword) are primitive strings.

  3. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings.

  4. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.property lookup.

Some of the useful methods for strings are as follow.

1. string.length()

const firstName = "Moreshwar";

console.log(firstName.length); // o/p: 9

console.log("Moreshwar".length); // o/p: 9
Enter fullscreen mode Exit fullscreen mode
  • Behind the sceen the method is been call'ed (i.e PREMITIVE Strings).

  • JS will automatically convert existing String to String Object with same content.

  • Hence, above example would be like new String("Moreshwar").

2. string.slice(BeginParamater,EndParameter)

console.log(airline.slice(2, 5));

  • Creates substring, if we do not specify the ENDSTRING everything will be extracted.
console.log(firstName.slice(2, 5)); // o/p: res 
Enter fullscreen mode Exit fullscreen mode
console.log("123456789".slice(-4)); // o/p: 6789
Enter fullscreen mode Exit fullscreen mode

3. string.split("parameter")

  • The split()method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.

For Ex.

console.log("Moreshwar".split());
console.log("CODE + DAILY".split("+")); // ['CODE','DAILY']
Enter fullscreen mode Exit fullscreen mode

4. string.join("parameter")

  • The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

For Ex.

let name = ["Mr.".toUpperCase(), "Moreshwar" ,"Pidadi"];
console.log(name.join()); // MR. Moreshwar Pidadi 
Enter fullscreen mode Exit fullscreen mode

5. string.padStart(targetLength, padString)

  • The padStart()method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

  • The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string.

const message = "Hello every one this is string 
method padStart() and padEnd()";
console.log(message.padStart(0, "*"));
console.log(message.padStart(50, "*").padEnd(35, "#"));

o/p:

****Hello every one this is string method padStart() and padEnd()
***Hello every one this is string method padStart() and padEnd()######
Enter fullscreen mode Exit fullscreen mode

6. string.repeat(number)

  • The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.
console.log("Moreshwar".repeat(5));

o/p: 
     Moreshwar
     Moreshwar
     Moreshwar
     Moreshwar
     Moreshwar
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
moreshwar
Moreshwar Pidadi

Posted on March 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Strings in JS
javascript Strings in JS

March 5, 2022