Front-End Best Practices: HTML5 Doctype, Semantic Markup & JS Performance

This guide covers essential front‑end techniques—from using the HTML5 DOCTYPE and semantic markup to proper heading hierarchy, table usage, image alt text, microformats, jQuery UI, JavaScript style rules, loop optimizations, GUID generation, and feature detection with Modernizr—helping developers write cleaner, faster, and more accessible web code.

21CTO
21CTO
21CTO
Front-End Best Practices: HTML5 Doctype, Semantic Markup & JS Performance

HTML5 DOCTYPE

Never omit a DOCTYPE; modern browsers treat the simple <!DOCTYPE html> as HTML5, triggering standards mode even in older browsers like IE6/7.

&lt;!DOCTYPE html&gt;

Write Valid, Semantic Markup

Start headings at <h2>, wrap paragraphs in <p>, and use <table> only for tabular data with proper <th> headers. Set cellpadding, cellspacing, and border to 0 and control styling via CSS.

&lt;table cellpadding="0" cellspacing="0" border="0"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Cell Header&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Cell Item&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;

Images Must Have Alt Text

Provide meaningful alt attributes for <img> tags; use an empty string for decorative icons.

&lt;img src="dog.gif" alt="Fido and I at the park!" /&gt; &lt;img src="bullet.gif" alt="" /&gt;

Microformats for Contact Information

Use the hCard microformat to make contact data machine‑readable.

&lt;span class="tel"&gt; &lt;span class="type"&gt;home&lt;/span&gt;: &lt;span class="value"&gt;+1.415.555.1212&lt;/span&gt; &lt;/span&gt;

jQuery and jQuery UI Widgets

Both libraries aim for consistent cross‑browser appearance and follow WCAG 2.0 and ARIA guidelines.

JavaScript Style Guidelines

Keep existing formatting unless reformatting the whole file. Prefer spaces around operators and braces on the same line.

// Bad if(blah==="foo"){ foo("bar"); } // Good if (blah === "foo") { foo("bar"); }

Always use === (strict equality) except when comparing to null, where == also matches undefined.

var zeroAsAString = "0"; if (zeroAsAString == 0) { /* true */ } if (zeroAsAString === 0) { /* false */ }

Specify the radix when using parseInt to avoid unexpected octal/hex parsing.

alert(parseInt("08")); // 2 alert(parseInt("08", 10)); // 8

Use descriptive Boolean variable names prefixed with is, can, or has.

Avoid Global Namespace Pollution

Encapsulate settings in a single object or use a namespaced variable.

// Bad – many globals var settingA = true; var settingB = false; // Good – single namespace var settings = { settingA: true, settingB: false }; // Access settings.settingA;

CamelCase Variable Naming

Use lower‑camelCase for variables; constants may be upper‑snake‑case.

var xPosition = obj.scrollLeft; // better var X_POSITION = 1; // constant

Loop Performance

Cache array length outside the loop and minimize DOM reflows by building HTML strings first.

for (var i = 0, len = arr.length; i < len; i++) { // loop body } var html = ""; for (var i = 0, len = arr.length; i < len; i++) { html += "<li>" + arr[i].title + "</li>"; } myList.innerHTML = html; // single reflow

Use break and continue to exit or skip iterations when appropriate.

Function Parameters

Pass a single object instead of many individual arguments.

function greet(user) { alert(user.name); } greet({ name: "Bob", gender: "male" });

Mapping this to self

Store the outer this in a variable (e.g., _self) before entering a callback.

var _self = this; $(_self.friends).each(function(i, item) { if (item.name === toFind) return item; });

Generating Unique IDs

Avoid using timestamps; prefer GUID/UUID functions.

function S4() { return (((1+Math.random())*0x10000)|0).toString(16).substring(1); } function guid() { return S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4(); } var myID = "static" + guid();

Feature Detection Instead of Browser Sniffing

Use Modernizr to test for HTML5 features like canvas rather than checking the user‑agent.

if (Modernizr.canvas) { /* canvas supported */ }

Readable Millisecond Values

Express timeouts as calculations for clarity, e.g., 30 * 1000 instead of 30000.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptaccessibilityHTML5
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.