It seems that \csname automatically defines the control sequence in question if it's not already defined. The code below creates a document creating "a b c"; swapping the \csname line and \foo line gives an error. I would have expected \csname to simply give an error if used with an undefined control sequence, but instead it appears it defines the sequence to something empty?
\documentclass{article}
\begin{document}
a
\csname foo\endcsname
b
\foo
c
\end{document}
It behaves exactly the same in pdflatex and lualatex, and with some syntactic differences the same behavior happens in pdftex and luatex as well. Is this expected behavior (and if so, where is it documented), and how can I work around it?

\ERRORis just a(ny) control sequence that does not exist. – Werner Sep 10 '20 at 20:20\csnamefor a system of cross-references and the reference is not yet defined. – egreg Sep 10 '20 at 20:52\ifcsname, but I suppose there's no point arguing about it. – Kyuuhachi Sep 10 '20 at 21:11