This is (at least) possible by means of using TeX conditionals.
You need to test #2 for emptiness. The example below uses a temporary macro to test against \@empty whether #2 is empty. As #2 only holds a few simply characters (no TeX), you could also simply do
\ifx\\#2\
to test for an empty string (if it’s empty \ifx\\\\ will be true); instead of \\ you can also use nearly any other control sequence that doesn’t come up in #2 (e.g. \relax).
eTeX allows us to do a safe test with \detokenize:
\if\relax\detokenize{#2}\relax
There are also many packages that provide test for empty strings:
In my example, I’m using \@firstoftwo and \@secondoftwo so that one doesn’t need to repeat the {red}{#2} part of your command. It is also preferable to use them for many other reason (see the references), especially if you want to nest \ifs.
Of course, in this simple example you don’t necessarily need this.
References
Code
\documentclass{beamer}
\makeatletter
\newcommand<>{\mycommand}[1]{%
\def\@tempa{#2}%
\ifx\@tempa\@empty
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\textcolor<2,4>}{\textcolor#2}{red}{#1}}
\makeatother
\begin{document}
\frame{\mycommand{test}}
\end{document}
\expandafter\@firstoftwoand\expandafter\@secondoftwoinstead of directly loading{\textcolor<2,4>{red}{#1}}and{\textcolor#2{red}{#1}}}afterifxandelse? – Lep Oct 12 '13 at 15:59#2. How you do that and what you do with that is basically just a question of taste, purpose and if you want to load packages. There are probably a few questions on how to test for empty strings. – Qrrbrbirlbel Oct 12 '13 at 16:47