Skip to content
Advertisement

syntax meaning of ‘ (method) alert(message?: any): void ‘ in ‘window.alert()’ in javascript?

In Visual Studio Code, while making a react app when I move my cursor on window.alert(), I see a pop up which reads as follows:

alert(message?: any): void; 
(method) alert(message?: any): void

Please explain what it means, as I am from java background and I can’t understand it. I could only understand that alert() is a method which returns nothing, but what about it’s parameters?

What is (message?: any)?

Advertisement

Answer

The closest Java equivalent would be

public void alert(Optional<Object> message){...}

What does ‘any’ mean?
java.lang.Object cannot extend primitive types, however, Typescript can with any.

Type any denotes the value can be any Object Type, including primitive types. For example it can be a primitive type such as boolean, string, or number, or any Class types such as React, Button, or JSXElement.

What is the question mark?
In the Javascript, the delimiter ? means the parameter is optional.
For example, both of these are acceptable:

  • alert() — No message argument
  • alert(“hello”) — One argument
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement