Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an exclamation mark in array index do?

While perusing through my organization's source repository I came across this little gem:

RawParameterStorage[!ParameterWorkingIdx][ParameterDataOffset] = ... 

Is this valid code? (It compiles) What does the exclamation mark here do?

An invert ~ operator might make sense, since it's commonly confused with the not ! operator in boolean expressions. However, it doesn't seem to make logical sense to impose the not ! operator on an array index. Any Thoughts?

like image 501
Jim Fell Avatar asked Sep 13 '16 13:09

Jim Fell


People also ask

What is '!' In TypeScript?

What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !

What does exclamation mark do in JavaScript?

In Javascript, the exclamation mark (“!”) symbol, called a “bang,” is the logical “not” operator. Placed in front of a boolean value it will reverse the value, returning the opposite. ! true; // Returns false.

Why do we use exclamation mark in angular?

The exclamation mark (non-null assertion) operator removes null and undefined from the type of an expression. It is used when we we know that a variable that TypeScript thinks could be null or undefined actually isn't. Copied!

What does exclamation mark mean in coding?

In programming and scripting languages, the exclamation mark is used as "not." For example, "! =" also means not equal. See our operator definition for further information. Used to help identify a nonexecutable statement.


1 Answers

!ParameterWorkingIdx Means ParameterWorkingIdx is 0, If it is, !ParameterWorkingIdx evaluates as true which might be implicitly converted to the indexer type (For example, 1 for integer indexer as in an array), otherwise, it evaluates as false.

  • If ParameterWorkingIdx == 0 then [!ParameterWorkingIdx] == [1].

  • If ParameterWorkingIdx != 0 then [!ParameterWorkingIdx] == [0].

It also depends on other stuff like:

  • The type of ParameterWorkingIdx.

  • overloading of ! operator by the type of ParameterWorkingIdx.

  • indexer overloading by the type of RawParameterStorage.

  • etc...

like image 155
Tamir Vered Avatar answered Oct 09 '22 11:10

Tamir Vered