最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Can we write IF statement without else in javascript - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Apr 21, 2016 at 6:21 ShaSha 1,9946 gold badges35 silver badges60 bronze badges 5
  • What is not working ? Looks good.... – Rayon Commented Apr 21, 2016 at 6:25
  • 3 The else part of an if-statement is optional. Your code is correct. – Lucas S. Commented Apr 21, 2016 at 6:26
  • it is working fine. but can i write a "if" statement without "else". is it a good practice? @RayonDabre – Sha Commented Apr 21, 2016 at 6:27
  • 2 of course it's good. Did you consider googling that before posting it here? It should be explained in a lot of places. – Mike B Commented Apr 21, 2016 at 6:28
  • It is perfectly fine.. But I would initialize $scope.noTask = something initially...depending on the length of the $scope.tasks – Rayon Commented Apr 21, 2016 at 6:28
Add a ment  | 

3 Answers 3

Reset to default 3

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

<p ng-show="tasks.length==0">No Tasks Available </p>

As in any (?) other programming language you can omit else part if it's empty.

Just make sure that if you assign some value in if (true) than if it's not executed have default value for that variable:

var test = false;

if (Math.rand() < 0.5) {
    test = true;
} 

Also there is shorthand for if else (where you can't omit else part):

var test = Math.rand() < 0.5 ? "True value" : "False value";

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

You don't need an else statement, you can use ng-if.

The ng-if statement is also available without an else condition.

You should be able to use this angular code:

$scope.deleteTask =  function(){
    $scope.tasks.splice(this.$index, 1);
};

And this part for the HTML:

<li ng-repeat="task in tasks track by $index">{{task}}  
     <button ng-click="deleteTask()">x</button>
</li>
<p ng-show="noTask" ng-if="tasks.length == 0">No Tasks Available </p>
发布评论

评论列表(0)

  1. 暂无评论