Skip to content
Advertisement

Can we write IF statement without else in javascript

I wrote some code to create an simple todo app in angular js. I have the following function code to remove the tasks from the list.

Javascript code

$scope.deleteTask =  function(){
    $scope.tasks.splice(this.$index, 1);
    if($scope.tasks.length < 1){
        $scope.noTask = true;
    }     
};

HTML code

    <li ng-repeat="task in tasks track by $index">{{task}}  <button ng- click="deleteTask()">x</button></li> </li>
    <p ng-show="noTask">No Tasks Available </p>

I wanted to show a message when there are no tasks in the list. i have achieved this using “if” statement. but i don’t need an else here. i am not sure whether its the right way. what will be the proper way to achieve this

Advertisement

Answer

There is nothing wrong with your code.
You can use the if statement without the else.
In your case I would recommend writing it as follows to remove some unnecessary code:

<p ng-show="tasks.length==0">No Tasks Available </p>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement