w3resource

JavaScript Validation - Exercises, Practice, Solution

JavaScript validation [10 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a JavaScript function to validate whether a given value type is boolean or not. Go to the editor

Click me to see the solution

2. Write a JavaScript function to validate whether a given value type is error or not. Go to the editor

Click me to see the solution

3. Write a JavaScript function to validate whether a given value type is NaN or not. Go to the editor

Click me to see the solution

4. Write a JavaScript function to validate whether a given value type is null or not. Go to the editor

Click me to see the solution

5. Write a JavaScript function to validate whether a given value is number or not. Go to the editor

Click me to see the solution

6. Write a JavaScript function to validate whether a given value is object or not. Go to the editor

Click me to see the solution

7. Write a JavaScript function to validate whether a given value type is pure json object or not. Go to the editor

Click me to see the solution

8. Write a JavaScript function to validate whether a given value is RegExp or not. Go to the editor

Click me to see the solution

9. Write a JavaScript function to validate whether a given value type is char or not. Go to the editor

Click me to see the solution

10. Write a JavaScript function to check whether given value types are same or not. Go to the editor

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Live Demo:

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.




JavaScript: Tips of the Day

Applies a function against an accumulator and each key in the object (from left to right)

Example:

const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
console.log(transform(
  { a: 1, b: 2, c: 1 },
  (r, v, k) => {
    (r[v] || (r[v] = [])).push(k);
    return r;
  },
  {}
)); // { '1': ['a', 'c'], '2': ['b'] } 

Output:

[object Object] {
  1: ["a", "c"],
  2: ["b"]
}