Converting Between Types & Manipulating Strings

Converting Between Types & Manipulating Strings

While watching a JS tutorial video via Pluralsight, I had a Eureka moment about my first blog post. I finally figured out what I was going to write about.

alina-grubnyak-ZiQkhI7417A-unsplash.jpg Strings!

I was intrigued by the shortcuts, methods, and operators used to manipulate strings and I thought to share. As you must know JS has 5 different data types but I'd be talking about strings because I think they're more fun.

Let's start with the:

  • typeof Operator: You can use this operator to find the data type of a JavaScript variable, i.e either strings, boolean, numbers etc

    For example typeof "Emi" Returns "string" typeof True Returns "boolean" typeof 350 Returns "number"

so whenever you're confused about the data type of a variable you're working with, you can summon the typeof operator.

  • toString(): This method can convert any type of numbers, literals, variables or expressions to strings. For example ``` (350).toString Returns "350"

Boolean false.toString() Returns "false"

- toLowerCase(): It converts a string to lowercase letters, it doesn't change the original string, it only converts it. Same with toUpperCase(), it converts the string to uppercase letters.
For Example
```"EMI".toLowerCase()
Returns "emi"
while 
"emi".toUpperCase()
Returns "EMI"
  • .length: This property returns the length of a string i.e the number of characters in the string. The length of an empty string is zero. For Example

    var str = "Hello World!";
    var n = str.length;
    Returns 12
    // This is because the whitespace in the middle is also counted.
    
  • .trim(): This method removes all whitespace from both sides of the string. It doesnt change the string, only removes the whitespace. For Example

    var str = "       Hello World!        ";
    alert (str.trim());
    Returns Hello World!
    

    This is just but to name a few. if you'd like to know more about strings methods take a look at the link below

    Thank you for reading this article!