JavaScript: String & Methods

JavaScript: String & Methods

ยท

3 min read

js.png

As we know our beloved Javascript comes with 8 primitive-types, and one of them is 'String' which is mostly used, although we daily deal with it but mostly we forget the methods or say functions that comes with it, hence this article to provide a cheatsheet that can be helpful for our programming journey.

Strings are textual data, enclosed within, single-quotes, double-quotes or backtick

const singleQuotes = 'I am a string';
const doubleQuotes = "i am also a string";
const backtickString = `I am a special string ๐Ÿ˜Ž` // Backticks allow us to embed any expression/variables into the string.

String Methods

  • string.charAt() : returns the character of specified position.
    const string = 'Hello, JS' ;
    console.log(string.charAt(7)); // 'J'
    
  • string.concat() : returns the concated strings.

    const string = 'Concat ';
    const anotherString = 'Me';
    const concatedString = string.concat(anotherString);
    console.log(concatedString); // 'Concat Me'
    
  • string.toUpperCase() : return the string in UpperCase.

    const string = 'hey, there!'
    console.log(string.toUpperCase()) // 'HEY, THERE'
    
  • string.toLowerCase() : return the string in LowerCase.
    const string = 'Whats, Up?';
    console.log(string.toLowerCase()); // 'whats, up?'
    
  • string.indexOf() : returns the index of string passed as arguement.
    const string = 'How you doin?';
    console.log(string.indexOf('w')) // 2
    console.log(string.indexOf('doin')) // 8
    
  • string.includes() : returns true or false if string found or not.
    const string = 'I'm good';
    console.log(string.includes('good')); // true
    
  • string.split() : returns an array of substrings of string.
    const string = 'What about you?';
    console.log(string.split('')); // [ ' W ', ' h ', ' a ', ' t ', ' a ', ' b ', ' o ', ' u ', ' t ', ' y ', ' o ', ' u ', ' ? ' ];
    console.log(string.split(' ')); // [ 'What about you?' ];
    
  • string.slice(start, end) : returns a part of string extracted from given string.
    const string = 'Done with conversations, now with emojis';
    console.log(string.splice(0,9)); // 'Done with'
    
  • string.trim() : returns the string with whitespace from front and back removed.
    const string = '      ๐Ÿ˜ฑ     ';
    console.log(string.trim()) //  ๐Ÿ˜ฑ
    

String methods with RegEx

  • string.search() : searches string for a specific value or a regular expression and returns position of the match, returns -1 if not found.

    const string = 'Find me';
    console.log(string.search('me'); // 5
    
  • string.match() : searches the string against a regular expression, and returns result into an array, if the regular expression doesn't have the g modifier (i.e. global search) then this method returns the first instance.

    const string = "Searching with RegEx";
    console.log(string.match(/R/gi); // [ ' r ', ' R ' ] , modifier i for case insensitive search.
    console.log(string.match(/search/gi); // [ 'search' ]
    
  • string.replace() : searches for a specific value, and returns the new string with replaced value.
    const string  = "I adore JS";
    console.log(string.replace('adore', 'love'); // 'I love JS'
    

Hope this helps in your developer's journey, bye for now ๐Ÿ‘‹

ย