w3resource

JavaScript: Return true if the specified value is null, false otherwise

JavaScript fundamental (ES6 Syntax): Exercise-196 with Solution

Check If Null

Write a JavaScript program that will return true if the specified value is null, false otherwise.

  • Use the strict equality operator to check if the value of val is equal to null.

Sample Solution:

JavaScript Code:

// Define a function 'isNull' that checks if the given value 'val' is null
const isNull = val =>
  // Check if 'val' is strictly equal to null
  val === null;

// Test cases to check if the values are null
console.log(isNull(null)); // true (the value is null)
console.log(isNull(123));  // false (the value is not null)

Output:

true
false

Visual Presentation:

JavaScript Fundamental: Return true if the specified value is null, false otherwise.
JavaScript Fundamental: Return true if the specified value is null, false otherwise.

Flowchart:

flowchart: Return true if the specified value is null, false otherwise

Live Demo:

See the Pen javascript-basic-exercise-196-1 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript program that returns true if a given value is exactly null.
  • Write a JavaScript function that distinguishes between null and undefined in its type check.
  • Write a JavaScript program that checks a variable and logs whether it is null, undefined, or neither.
  • Write a JavaScript function that returns a custom message if a value is null.

Go to:


PREV : Check If Number.
NEXT : Check String Lowercase.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.