This is a JavaScript guideline for learning. It focuses on clean, fast, and modern browser-side coding. All examples follow a direct, no-frills style.
This is based on web4now guideline.
// Save JavaScript files with `.js` extension
// Single line comment
variable = value;
table = ["item1", "item2"];
You can assign tables to variables or include variables inside tables.
table_in_var = table;
var_in_table = [var1, var2, var3];
Functions are written as actions. Sub-actions are attached as properties.
act = () => {};
sub.act = () => {};
Temporal are temporary and run immediately this is also use to open system of js.
(() => {
})();
Use setInterval
for repeated actions.
This is part of the browser’s system and is reliable.
setInterval(() => {
// repeated code
}, milliseconds);
Use setTimeout
for delayed actions.
setTimeout(() => {
// delayed code
}, milliseconds);
Use ? :
for short conditionals.
condition ? doThis() : doThat();
Example:
el.exists
? (() => {
// if true
})()
: (() => {
// if false
)();
For replacing characters or rebuilding strings:
string.split('').join('');
The browser already provides insertAdjacentHTML
.
You do not need .append()
or .innerHTML += …
.
This is clean, safe, and fast.
Usage:
el.insertAdjacentHTML('beforeend', '<span>content</span>');
It supports four positions:
'beforebegin'
— before the element itself'afterbegin'
— just inside the element, before its first child'beforeend'
— just inside the element, after its last child'afterend'
— after the element itselfExample:
el.insertAdjacentHTML('beforeend', '<section>New</section>');
Use literal attributes to store state or meaning.
There is no need to use data-*
attributes — modern browsers support literal attributes and they are faster.
Example:
<section state="active"></section>
Do not overuse <div>
. Use proper elements like <section>
, <header>
, <main>
, <footer>
, <button>
, <input>
.
For clean and fast code, write events directly in HTML. This reduces overhead and improves performance.
<input oninput="myAction()" />
<main onmousedown="myAction()"></main>
<body onkeydown="myAction()"></body>
Use the browser’s built-in toggleAttribute()
method.
el.toggleAttribute('state');
class
keyword or .classList
.data-*
attributes — use literal attributes.<div>
elements — use meaningful HTML tags.insertAdjacentHTML()
— no wrappers needed.oninput
, onmousedown
, onkeydown
).toggleAttribute()
.setInterval
and setTimeout
for timing.lol