StudyGPT Just Leveled Up – Say Hello to Knowee! 🚀
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

🧐 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

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.