JavaScript: Check if String Starts with Space

A general requirement many people encounter while working with javascript strings is to check if the first letter of the string is a space. This article demonstrates easy ways to check if the string starts with a space using different methods and illustration examples.

Table of Contents:

Check If String Starts with Space using RegEXP and test()

JavasScript’s test() method will execute a search for a match between the regular expression and the specified string. This method takes an argument that is a string against which the regular expression is matched. If the match is found, true is returned. Else, false is returned.

Syntax: test(string)

Example:-

Advertisements

Check if the below strings start with space

  • “JAVASCRIPT”
  • ” JAVASCRIPT”

Code:-

function checkFirstLetterSpace(_string)
{
return /^\s/.test( _string);
}
//examples
let dummyString1 = "JAVASCRIPT";
let dummyString2 = " JAVASCRIPT";
//usage of the function 
console.log(checkFirstLetterSpace(dummyString1));
console.log(checkFirstLetterSpace(dummyString2));

Output:-

false
true

Explanation:-

  • Here in the above code test() method is used to match the pattern against the _string passed as an argument, the regular expression is /^\s/
  • / and / specify the start and end of the expression.
  • ^ specify the beginning of the string.
  • \s specify that character are the special characters.
  • If the expression is matched, then return true; otherwise, false is returned.

Check If String Starts with Space using RegEXP and match()

JavaScript’s match() method will return the result of a string matching against a regular expression. This method takes a regular expression object as a parameter and returns an array of matching results. 

Syntax: match(regExp)

Example:-

Check if the below strings start with a space

  • “JAVASCRIPT”
  • ” JAVASCRIPT”

Code:-

function checkFirstLetterSpace(_string)
{
    return _string.match(new RegExp( /^\s/)) !== null;    
}
//examples
let dummyString1 = "JAVASCRIPT";
let dummyString2 = " JAVASCRIPT";
//usage of the function 
console.log(checkFirstLetterSpace(dummyString1));
console.log(checkFirstLetterSpace(dummyString2));

Output:-

false
true

Explanation:-

  • The method match() is called here in the above code by the calling string. The regular expression /^\s/ is passed as an argument to the match() method  . 
  • / and / specify the start and end of expression.
  • ^ specifies the beginning character.
  • /^\s/ specifies that character is space
  • If the first character is a space from the pattern, the matching expression is returned. Else, null is returned. 
  • Hence, if something is retrieved, the first character is a space. Otherwise, it is not.

Check If String Starts with a Space using ASCII Code

ASCII codes are the numeric value given to the characters and symbols for storing and manipulation by computers.

JavaScript’s charCodeAt() method will return an integer ranging from 0 and 65535 that depicts the UTF-16 code value at the given index.

Example:-

Check if the below strings start with space

  • “JAVASCRIPT”
  • ” JAVASCRIPT”

Code:-

function checkFirstLetterSpace(_string) 
{
    if (_string.charCodeAt(0) === 32 )
        {
            return true;
        }
        else return false;
}
//examples
let dummyString1 = "JAVASCRIPT";
let dummyString2 = " JAVASCRIPT";
//usage of the function 
console.log(checkFirstLetterSpace(dummyString1));
console.log(checkFirstLetterSpace(dummyString2));

Output:-

false
true

Explanation:-

  • Here in the above code, we check if the character’s ASCII code placed at 0th index is 32 as the ASCII code for space is 32.
  • This check is made using the method charCodeAt(0).

I hope this article helped you check if the first letter is a space in a javascript string. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top