when do i use pinMode?

do i use it only on digital pins?

Yes. You use it to set a digital pin as input or output.

And you can enable pull up resistors.

pinMode(myPin,INPUT_PULLUP);

.

If used on the analog pins that also have a digital side (A0-A5 on 328p),
you can set these to OUTPUT (making them a normal digital output), or to INPUT_PULLUP
and read a resistor (LDR, NTC, PTC) connected directly to GND using the (ca 50k) internal pullup
as the top part of a voltage divider.

Pins default to being inputs, so unless you want to use them as outputs, use the analogue pins as digital inputs or use INPUT_PULLUP you don't actually need to use pinMode() at all.

However, using it does no harm and it tends to reinforce what you are intending to do when reading the code.

UKHeliBob:
Pins default to being inputs, so unless you want to use them as outputs, use the analogue pins as digital inputs or use INPUT_PULLUP you don't actually need to use pinMode() at all.

However, using it does no harm and it tends to reinforce what you are intending to do when reading the code.

Better than that, it documents in setup() which pins are used for what - stops you reusing a
pin for two purposes by mistake if you explicitly call pinMode() on it - its like declaring the pin.

Name pins well