How to enter the input of line 4 below the code

  angular.module('myReverseFilterApp', [])
    .filter('reverse', function() { 
           return function(input, uppercase) {
             input = input || ''; //  declaring the variable
                var out = '';
               for (var i = 0; i < input.length; i++) {
              out = input.charAt(i) + out;
            }
          // if condition for uppercase
            if (uppercase) {
              out = out.toUpperCase();
            }
            return out;//return statement
          };
        });

The code defines a filter for AngularJS that takes a string as input and returns the string in reverse order, as well as uppercase.

I could not understand the code written in line number 4, which takes input:

input = input || ''; //  declaring the variable

It will be useful if someone can describe to me how to enter the input of line 4.

+4
source share
1 answer

Line 4 can almost be replaced by the following guard statement, which may make more sense to you:

if (input === undefined) {
  input = '';
}

What input = input || ''really means is that if input falsy, set inputto an empty string to avoid things falling down.

, javascript , , input, input undefined, . 3.

+3

Source: https://habr.com/ru/post/1681698/


All Articles