Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact difference between ?. and ?: operators in angular

I am not thinking about ?:(ternary operator). Sometimes I am watching in YouTube tutorials where people are using ?. Operator in HTML pages, and sometimes they are using ?: in TS(typescript) pages. I am not so clear how exactly they are different?

like image 839
Rajeshkumar Gone Avatar asked Feb 21 '19 20:02

Rajeshkumar Gone


People also ask

What is == and === in Angular?

== is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

What is the use of === in Angular?

Two objects or values are considered equivalent if at least one of the following is true: Both objects or values pass === comparison. Both objects or values are of the same type and all of their properties are equal by comparing them with angular. equals .

What is the difference between ||?

In short, the difference between the two operators boils down to the difference between falsy and null/undefined . Where the logical or ( || ) operator takes the right operand in the case of a falsy value — which includes empty string, 0, false, NaN, etc. The nullish coalescing operator ( ?? )

What is '!' In Angular?

The Angular non-null assertion operator (!) When the Angular compiler turns your template into TypeScript code, it prevents TypeScript from reporting that hero.name might be null or undefined . Unlike the safe navigation operator, the non-null assertion operator does not guard against null or undefined .


2 Answers

So there is a difference when using the ? within Angular, here are the three ways you may be referring to for the usage.

safe-operators

When you set a value within the HTML with a question mark in it, this is a safe check, so you check the variable is defined before you access it. (trying at access values the do not exist will result in errors).

The snippet below would check this.example has a value before checking for text which would result in an error. If text is accessed while undefined this can almost assure unwanted behavior.

<p>{{example?.text}}</p>

This keeps everything safe, to read more about safe operators, have a read through the Angular docs found here

Optional Parameters

The next use which is what I think you were looking for is optional values in functions / interface. This means that the interface will not throw an error if it is called without the exampleValue as it has now been defined as optional.

export interface Itest
{
 exampleValue?: string; // optional
 neededValue: string; // non-optional
}

Or within a function, without the optional indicator (?) an error would occur if the function was called like. this.exampleFunction();

public exampleFunction(test?): void 
{
  console.log(test);
  // this function can be called with or without test being passed in without causing an error.
}

More examples of this can be found in this short article about Optional Parameters

Conditional (ternary) operator

The question was not looking for this but thought it would make sense to pop it in as its another case where the ? can be seen in use.

When seen in typescript you can use it in a conditional ternary statement (if / else) looking like so.

const example = 'hello';
console.log(example === 'hello' ? 'im true' : 'im false');

which would be the same as writing the following statement.

    const example = "hello";
    
    if (example === 'hello')
    {
      console.log('im true');
    }else 
    {
      console.log('im false');
    }

More information and uses of the Conditional (ternary) operator can be found here.

like image 84
Dince12 Avatar answered Sep 30 '22 19:09

Dince12


?. is a safe-navigation operator. It simply makes it so no errors will be thrown if the preceding value is null. There are some nitty-gritty details at play, but it can basically be thought of as a null-checking ternary expression.

<div>{{item?.value}}</div>

... is roughly the same as:

<div>{{item ? item.value : null}}</div>
like image 32
StriplingWarrior Avatar answered Sep 30 '22 20:09

StriplingWarrior