-1

With JavaScript, it's best practice to use semicolons after a function expression block:

var myfunction = function() {
    alert('hello world');
};

What about in AngularJS, say in a controller, using the dreaded $scope? Often times I see these written without semicolons after the closing block:

$scope.myFunction = function() {
    alert('hello world');
}

But does anyone know the best practice for this? Will adding a semicolon at the end of these $scope 'expressions' break my AngularJS app? Like so:

$scope.myFunction = function() {
    alert('hello world');
};
Jimenemex
  • 3,104
  • 3
  • 24
  • 56
Kyle Vassella
  • 2,296
  • 10
  • 32
  • 62
  • 1
    AngularJS is just a javascript framework. You are free to use or not use a semicolon and this will largely be a matter of personal preference and opinion. – Lex Jun 22 '18 at 16:33
  • To tack on to @Lex comment, find whatever you're comfortable with and use a linter to enforce. – Phix Jun 22 '18 at 16:42

1 Answers1

-1

I almost always don't use a semi colon when declaring a function. Regardless of it's a normal Javascript function or a AngularJS function. This is my preference and the preference of many others, but I would go by the current coding standards of what you are currently working on. If it uses ; then use them also to be consistent. If you are working alone, then do what feels comfortable for you.

What I do realize though is that it can be quite confusing to keep track of all the {} when inside an AngularJS controller which has objects in it also. What to remember to do is always add a ; when you finish declaring an object. Something I overlook sometimes when I see

$scope.something = ...

I overlook it sometimes thinking it's a function but can really be an object. For example,

var MyController = function($scope) {
    $scope.Obj = {
        name: 'Foo' 
    }; // Don't forget me!

    $scope.DoSomething = function(param) {
        $scope.Obj = {
            name: param
        }; // Don't forget me!
    }
}

This is just something I have come across in my code and in seeing others.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56