Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can I use arrow javascript function before its initialization

I checked out Why can I use a function before it's defined in JavaScript?. and It says I can use functions before or after initialization when using Declaration Functions. However for the case of Expression Functions I need to initialize before use.

But check my code please. I used Expression Functions and I can use it even before initialization.

const x = () => {
  console.log('hello x')
  y() // why can I use it here???
}

const y = () => {
  console.log('hello y')
}

// later use somewhere in HTML
x()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because it is just a definition.

If you write x() before the definition of y, there would be an error.

const x = () => {
  console.log('hello x')
  y() // why can I use it here???
}

x() // error

const y = () => {
  console.log('hello y')
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading