To my understanding, after ES6 and the introduction of let
and const
, a variable declared with them must be declared before it's used. However, I have run into many examples where my code runs just fine when I declare the variables after its use. For example, this is the code from (a simplified version of) the TypeScript template of React Native:
import React from 'react';import {StyleSheet, Text} from 'react-native';const App = () => { return (<><Text style={styles.text}>Some text</Text></> );};const styles = StyleSheet.create({ text: { backgroundColor: 'blue', },});export default App;
Here, styles
are declared after they are used, and it even seems to be the idiomatic way to do it, but even ESLint highlights as no-use-before-define
.
My question is: why does it still work?