Knowee
Questions
Features
Study Tools

Write a JavaScript function to check whether a string is blank or not.Test Data :console.log(is_Blank(''));console.log(is_Blank('abc'));truefalse

Question

Write a JavaScript function to check whether a string is blank or not.

Test Data:

console.log(is_Blank(''));  // Output: true
console.log(is_Blank('abc'));  // Output: false
🧐 Not the exact question you are looking for?Go ask a question

Solution

Problem Breakdown

  1. We need to create a function named is_Blank.
  2. The function should take a single string parameter.
  3. It should check if the string is either empty or consists only of whitespace characters.
  4. Return true if the string is blank; return false otherwise.

Relevant Concepts

  • A string is considered blank if it is empty ('') or contains only spaces (' ').

Analysis and Detail

  1. We'll use the trim() method to remove whitespace from both ends of the string.
  2. If the result is an empty string, we will consider it blank.

Implementation

Here's the JavaScript function based on the above analysis:

function is_Blank(str) {
    return str.trim() === '';
}

// Test Data
console.log(is_Blank(''));      // true
console.log(is_Blank('abc'));   // false

Verify and Summarize

  • For the input '', str.trim() gives '', so it returns true.
  • For the input 'abc', str.trim() gives 'abc', so it returns false.

Final Answer

The function correctly checks whether a string is blank or not. The output is:

  • true for an empty string.
  • false for a non-empty string like 'abc'.

This problem has been solved

Similar Questions

Which Excel formula would you use to find out if a specific cell is blank or not?=BLANK=ISEMPTY=ISBLANK

Which of the following is a falsy value in JavaScript?Options0 (number)true (boolean)[ ] (empty array)"false" (string)

Which of the following is a falsy value in JavaScript?Options[ ] (empty array)"false" (string)0 (number)true (boolean)

What is the output of typeof null in JavaScript?Options"null""undefined""string""object"

blankis a number that can be divided exactly by six.When it is divided by five, four is the number left over

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.