1. Indentation

function sayHello() {
	if ( true ) {
		console.log( 'Hello, World!' );
	}
}

2. Quotes

const greeting = 'Hello, World!';
const html = '<div class="container"></div>';

3. Naming Conventions

let userName = 'JohnDoe';
function getUserName() {
	return userName;
}
 
class UserProfile {
	constructor( name ) {
		this.name = name;
	}
}

4. Line Length

const message = 'This is a long message that should be split across lines ' +
	'to maintain readability and adhere to the 80-character limit.';

5. Comments

// Redirect users to the homepage if they are not logged in
function checkUserLoginStatus() {
	if ( ! isLoggedIn() ) {
		redirectToHomepage();
	}
}

6. Equality Checks

if ( user.id === 10 ) {
	console.log( 'User ID is 10' );
}

7. JSDoc Comments

/**
 * Calculates the area of a rectangle.
 *
 * @param {number} width - The width of the rectangle.
 * @param {number} height - The height of the rectangle.
 * @returns {number} The area of the rectangle.
 */
function calculateArea( width, height ) {
	return width * height;
}

8. Spacing

const sum = a + b;
const coordinates = [ 10, 20 ];

9. Array and Object Literals

const settings = {
	theme: 'dark',
	layout: 'grid',
	showSidebar: true,
};
 
const fruits = [
	'apple',
	'banana',
	'orange',
];

10. Avoid Global Variables

(function() {
	const localVariable = 'I am private';
	function privateFunction() {
		return localVariable;
	}
})();

11. Use let and const

const maxItems = 10;
let currentItem = 0;
 
while ( currentItem < maxItems ) {
	currentItem++;
}

Let's dive even deeper into WordPress JavaScript coding standards:

12. Function Declarations vs. Expressions

function greetUser( name ) {
	return `Hello, ${name}!`;
}
const greet = function( name ) {
	return `Hello, ${name}!`;
};

13. IIFE (Immediately Invoked Function Expressions)

(function() {
	const privateVar = 'This is private';
	console.log( privateVar );
})();

14. Object-Oriented Programming (OOP)

class Person {
	constructor( name, age ) {
		this.name = name;
		this.age = age;
	}
 
	sayHello() {
		return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
	}
}
 
const john = new Person( 'John Doe', 30 );
console.log( john.sayHello() );

15. Error Handling

try {
	const data = JSON.parse( jsonString );
	console.log( data );
} catch ( error ) {
	console.error( 'Parsing error:', error );
}

16. Modularization

// mathUtils.js
export function add( a, b ) {
	return a + b;
}
 
export function subtract( a, b ) {
	return a - b;
}
 
// main.js
import { add, subtract } from './mathUtils';
 
console.log( add( 5, 3 ) );  // 8
console.log( subtract( 5, 3 ) );  // 2

17. Event Handling

document.querySelector( '#parent' ).addEventListener( 'click', function( event ) {
	if ( event.target.matches( '.child' ) ) {
		console.log( 'Child element clicked!' );
	}
});

18. DOM Manipulation

const div = document.createElement( 'div' );
div.className = 'container';
div.textContent = 'Hello, World!';
document.body.appendChild( div );

19. Performance Considerations

function debounce( func, wait ) {
	let timeout;
	return function( ...args ) {
		clearTimeout( timeout );
		timeout = setTimeout( () => func.apply( this, args ), wait );
	};
}
 
window.addEventListener( 'resize', debounce( () => {
	console.log( 'Resized!' );
}, 200 ) );

20. Security Practices

function sanitizeInput( input ) {
	return input.replace( /<script.*?>.*?<\/script>/gi, '' );
}
 
const userInput = '<script>alert("XSS")</script>';
const safeInput = sanitizeInput( userInput );
console.log( safeInput );  // ""