What is the default setting for a digital output pin
On the AVR-based Arduinos at least, pins start in the INPUT, i.e. high
impedance state. If you then pinMode(pin, OUTPUT);, the pin turns to
OUTPUT LOW. Note, however, that if you first set the pin to
INPUT_PULLUP, and then to OUTPUT, the pin ends up in the
OPUTPUT HIGH state.

Sometimes you want to control the state the pin will have right when
it's turned to OUTPUT. You may for example simulate an open collector
output by switching between INPUT (or INPUT_PULLUP) and OUTPUT
LOW, while avoiding the OUTPUT HIGH state. In this case, a safe
idiom is to set the output level before switching the pin to OUTPUT,
e.g.

// Set pin to OUTPUT LOW without going throught the OUTPUT HIGH state.
digitalWrite(pin, LOW); // do this first
pinMode(pin, OUTPUT);


A side effect of performing a digitalWrite() on a pin that is not set
to OUTPUT is that the pin gets switched to either INPUT (if writing
LOW) or INPUT_PULLUP (if writing HIGH). This is an idiosyncrasy of
the AVR IO ports you should probably not rely on if you want your code
to be readable or portable.

Reference: the source code of pinMode() in the Arduino
core, and the datasheet of the relevant MCU. See for example the section
Ports as General Digital I/O in the ATmega328P’s datasheet.