2

I cant understand one thing in the code below - why (limit) is in parenthesis?

import { Pipe, PipeTransform } from '@angular/core'

@Pipe ({
 name: 'summary'
})

export class SummaryPipe implements PipeTransofm {
 transform(value: string, limit?: number) {
  if (!value) 
   return null;

  let specificLimit = (limit) ? limit : 50;
  return value.substr(0, specificLimit);
 }
}

Thanks

Here a screenshot, to be sure I copied it right:

enter image description here

4
  • are you sure you copied that part (limit) ? : 50; correctly? it seems to be an incorrect syntax Commented Jul 14, 2017 at 6:31
  • @Maximus, I am sure, I added a screenshot from the video of the Tutorial, from Udemy.com - udemy.com/angular-2-tutorial-for-beginners Commented Jul 14, 2017 at 6:33
  • 1
    yeah, that's a different case, it is now correct syntax. the parenthesis are just a style preference, they make more sense for me when there's an expression inside like (limit === 3) ? limit : 50 Commented Jul 14, 2017 at 6:40
  • @Maximus, Ok, thanks. Now I understand it.. Commented Jul 14, 2017 at 6:42

1 Answer 1

1

This code will throw syntax error because the true condition lacks an expression. The parenthesis are not needed in your example, you could write it like

let specificLimit = limit ? : 50;

too but like I said this will throw an error. You must give it a value for when the ternary yields true so something like:

let specificLimit = limit ? limit : 50;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.