var
The HTML <var> element marks a variable in a mathematical expression, program, or other technical explanation. It identifies a value that can change.
What you will learn
- What changing value or variable
varrepresents - How to use it in mathematical and programming examples
- How it differs from
codeandsamp
Basic mathematical example
<p>The area is <var>w</var> × <var>h</var>.</p>
The letters w and h represent values that can change. Use surrounding text to explain what each variable means.
Programming example
<p>The loop runs while <var>count</var> is less than the limit.</p>
var describes the role of a name in the explanation; it does not declare a variable or execute code. The actual declaration belongs in a code example, not in the HTML semantics.
var, code, and samp are different
var- A variable or placeholder whose value may change.
code- A fragment of program source or code-like syntax.
samp- Output produced by a program or system.
<p>Set <var>count</var> in the <code>for</code> loop.</p>
<p>The result is <samp>42</samp>.</p>
Explain variables clearly
A variable without context is difficult to understand. Introduce its meaning, units, allowed range, or relationship to other variables. For a long formula, a list or a separate explanation may be clearer than a dense inline sentence.
Common mistakes
- Using
varfor every identifier in a code sample. - Expecting it to define or change a JavaScript variable.
- Using it for program output instead of
samp. - Marking a symbol without explaining what its value represents.