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
Solution
Problem Breakdown
- We need to create a function named
is_Blank
. - The function should take a single string parameter.
- It should check if the string is either empty or consists only of whitespace characters.
- Return
true
if the string is blank; returnfalse
otherwise.
Relevant Concepts
- A string is considered blank if it is empty (
''
) or contains only spaces (' '
).
Analysis and Detail
- We'll use the
trim()
method to remove whitespace from both ends of the string. - 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 returnstrue
. - For the input
'abc'
,str.trim()
gives'abc'
, so it returnsfalse
.
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'
.
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
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.