ItSolutionStuff.com

AngularJS Check Null Empty or Undefined Variable Value Example

By Hardik Savani • October 20, 2023
Angular

you don't know how to check variable or object is null empty or undefined in angular js then i would like to help you to check undefined or null variable or object in angularjs. we can determine easily to variable is empty or not in angularjs.

Few months ago one developer send me email and he say me to i am working on my angularjs application and i need to check variable value is empty or null then both how can i do it. he also send me some code that he tried out. actually his code is also was working but i said him this is not way to you are checking object is empty, null or undefined in angularjs.

I said him and also send him solution that i am sharing with you. It's very simple and easy way we can do it using code jquery logic.

So basically, you can check it as like bellow code, i also write full example so you can check and see how it works.

if (!$scope.myVar) {

// If variable is empty, null and undefined

}else{

// If not

}

Example:

<!DOCTYPE html>

<html>

<head>

<title>Check null empty or undefined Angularjs Example - ItSolutionStuff.com</title>

<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

</head>

<body>

<div ng-app="mainApp" ng-controller="myController">

</div>

<script type="text/javascript">

var app = angular.module("mainApp", []);

app.controller('myController', function($scope, $timeout) {

$scope.myNullVar = null;

if (!$scope.myNullVar) {

console.log('variable is a null.');

}else{

console.log('variable is a not null.');

}

$scope.myEmptyVar = '';

if (!$scope.myNullVar) {

console.log('variable is a empty.');

}else{

console.log('variable is a not empty.');

}

if (!$scope.myUndefinedVar) {

console.log('variable is a undefined.');

}else{

console.log('variable is a not undefined.');

}

});

</script>

</body>

</html>

Output:

variable is a null.

variable is a empty.

variable is a undefined.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Current Date and Time in AngularJS?

Read Now →

AngularJS Convert String to Object Example

Read Now →

AngularJS Filter Change Date Format in Controller Example

Read Now →

AngularJS Remove Duplicates Object from Array Example

Read Now →

AngularJS Check and Uncheck All Checkboxes Example

Read Now →

AngularJS Check Checkbox is Checked or Not Example

Read Now →

AngularJS - How to Create Read More/Less Toggle using Directive?

Read Now →

AngularJS Nl2br Filter for Textarea Content Example

Read Now →

AngularJS Image Upload with Preview Example

Read Now →

How to Hide Div After Some Time in AngularJS?

Read Now →

How to Call Function on Page Load in AngularJS?

Read Now →

AngularJS Simple Datepicker Directive Example Tutorial

Read Now →

AngularJS - How to Limit String Length using Filter?

Read Now →

AngularJS - How to Capitalize the First Letter of Word Example

Read Now →

How to Remove HTML Tags from String in AngularJS?

Read Now →