What do you understand by Angular expressions? How are Angular expressions different from JavaScript expressions?

Angular expressions are code snippets that are used to bind application data to HTML. Angular resolves the expressions, and the result is returned to where the expression is written. Angular expressions are usually written in double braces: {{ expression }} similar to JavaScript.


{{ expression }}

Following is a list of some differences between Angular expressions and JavaScript expressions:

1. The most crucial difference between Angular expressions and JavaScript expressions is that the Angular expressions allow us to write JavaScript in HTML. On the other hand, the JavaScript expressions don’t allow.

2. The Angular expressions are evaluated against a local scope object. On the other hand, the JavaScript expressions are evaluated against the global window object. We can understand it better with an example. Suppose we have a component named test:

  1. import { Component, OnInit } from ‘@angular/core’;  
  2. @Component({  
  3. selector: ‘app-test’,  
  4. template: `  
  5. <h4>{{message}}</h4>,  
  6. styleUrls: [‘./test.component.css’]  
  7. })  
  8. export class TestComponent implements OnInit {  
  9. message:string = ?Hello world?;  
  10. constructor() { }  
  11. ngOnInit() {  
  12. }  
  13. }  

In the above example, we can see that the Angular expression is used to display the message property. In the present template, we are using Angular expressions, so we cannot access a property outside its local scope (in this case, TestComponent). This proves that Angular expressions are always evaluated based on the scope object rather than the global object.

3. The Angular expressions can handle null and undefined, whereas JavaScript expressions cannot.

See the following JavaScript example:

  1. <!DOCTYPE html>  
  2. <html lang=”en”>  
  3. <head>  
  4.     <meta charset=”UTF-8″>  
  5.     <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>  
  6.     <title>JavaScript Test</title>  
  7. </head>  
  8. <body>  
  9.     <div id=”foo”><div>  
  10. </body>  
  11. <script>  
  12.     ‘use strict’;  
  13.     let bar = {};  
  14.     document.getElementById(‘foo’).innerHTML = bar.x;  
  15. </script>  
  16. </html>  

After running the above code, you see undefined displayed on the screen. Although it’s not ideal to leave any property undefined, the user does not need to see this.

Now see the following Angular example:

  1. import { Component, OnInit } from ‘@angular/core’;  
  2. @Component({  
  3.   selector: ‘app-new’,  
  4.   template: `  
  5.       <h4>{{message}}</h4>       `,  
  6.   styleUrls: [‘./new.component.css’]  
  7. })  
  8. export class NewComponent implements OnInit {  
  9.   message:object = {};  
  10.   constructor() { }  
  11.   ngOnInit() {  
  12.   }  
  13. }  

In the above example, you will not see undefined being displayed on the screen.

4. In Angular expressions, we cannot use loops, conditionals, and exceptions. The difference which makes Angular expressions quite beneficial is the use of pipes. Angular uses pipes (known as filters in AngularJS) to format data before displaying it.