javascript - Why does "typeof" not need parentheses? -


is there difference between typeof (myvariable) compared typeof myvariable?

both work, coming php, don't understand why function can use parenthesis or not.

the typeof keyword represents operator in javascript programming.

the correct definition typeof operator in specification :

typeof[(]expression[)] ; 

this reason behind use of typeof typeof(expression) or typeof expression.

coming why has been implemented such let developer handle level of visibility in code. such, possible use clean conditional statement using typeof :

if ( typeof myvar === 'undefined' )   // ... ; 

or stating more complex expression using () sake of readability :

var istrue = ( typeof ( myvar = anothervar ) !== 'undefined' ) && ( myvar === true ) ); 

edit :

in cases, using parentheses typeof operator makes code written less prone ambiguity.

take instance following expression typeof operator used without parentheses. typeof return type of result of concatenation between empty string literal , number, or type of string literal ?

typeof "" + 42 

looking @ definition of operator stated above , precedence of operators typeof , +, appears previous expression equivalent :

typeof("") + 42 // returns string `string42` 

in case, using parentheses typeof bring more clarity trying express :

typeof("" + 42) // returns string `string` 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -