Null-check in javascript
In this article,we are going to make a program which efficently checks null values, undefined, empty array, empty string, false value and NaN. These values will give a tough time if you have not checked it effeciently in programming.
We are you using reactjs for DOM manipulation but you don't worry, our main focus is on handle null check in javascript. For learing reactjs visit here.
App.js file is displayed below:
This is our reactjs file in which only DOM manipulation is done.
1import "./styles.css";
2import React from 'react'
3import Null, from './null'
4 export default class App extends React.Component {
5 constructor(props)
6 {
7 super(props)
8 this.state={
9 Data:"",
10 output:""
11 }
12 };
13 getNull=()=>{
14 if(Null(this.state.Data))
15 {
16 this.setState({
17 output:"Null executed"
18 })
19 }else{
20 this.setState({
21 output:"Null not executed"
22 })
23 }
24 }
25 render() {
26 return <><button onClick={()=>this.getNull()}>Click here</button><h1>{this.state.output}</h1></>;
27 }
28}
Null.js file is displayed below
This is our main file where we are checking null values in javascript.
1const Null = (value) => {
2 if (
3 value === '' ||
4 value === 0||
5 value === 'Null' ||
6 value === false||
7 value === NaN ||
8 value === null ||
9 value === undefined ||
10 value.length === 0 ||
11 value === -1 ||
12 value === ""
13 ) {
14 return true
15 } else {
16 return false
17 }
18}
19
20export default Null
Here is the link of live example which we done so far:
Click here
About
Moiz is a software engineer from Arid University with a passion for writing tech tutorials and doing coding. I am continously produce JavaScript and other web development technology tutorials in concepts through easy-to-understand explanations written in plain English.I have expertise in next js ,react js ,javascript,html,bootstrap and node js.
Follow him on TwitterHassan is a software engineer from kohat university of science and technology with a passion for writing tech tutorials and doing javascript practices on daily basis.I have expertise in next js ,react js ,javascript,html,bootstrap and node js. Problem Solving and making concpets is my strong point.
Follow him on TwitterTags
Click to see all tutorials tagged with: