Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Best Practices

Avoid using global variables, the new keyword, ==, and eval().

Avoid Global Variables

Minimize the use of global variables, including data types, objects, and functions.

Global variables and functions can be overwritten by other scripts.

Instead, use local variables and leverage closures to manage scope effectively.

Always Declare Local Variables

All variables used within a function should be declared as local variables.

Local variables must be declared using the var, let, or const keyword; otherwise, they will become global variables.

Declarations on Top

It is a good practice to place all declarations at the top of each script or function.

This approach will:

  • Create cleaner code.
  • Provide a single location for viewing local variables.
  • Help avoid unintended (implied) global variables.
  • Reduce the risk of accidental re-declarations.
// Declare at the beginning
let firstName, lastName, price, discount, fullPrice;

// Use later
firstName = “John”;
lastName = “Doe”;

price = 19.90;
discount = 0.10;

fullPrice = price – discount;

This also applies to loop variables.

for (let i = 0; i < 5; i++) {

Initialize Variables

It is good practice to initialize variables when declaring them.

This will:

  • Result in cleaner code.
  • Provide a single place to initialize variables.
  • Help avoid undefined values.
// Declare and initiate at the beginning
let firstName = “”;
let lastName = “”;
let price = 0;
let discount = 0;
let fullPrice = 0,
const myArray = [];
const myObject = {};

Declare Objects with const

Declaring objects with const prevents unintentional changes to their type.

Example

let car = {type:“Fiat”, model:“500”, color:“white”};
car = “Fiat”;      // Changes object to string
const car = {type:“Fiat”, model:“500”, color:“white”};
car = “Fiat”;      // Not possible

Declare Arrays with const

Declaring arrays with const prevents accidental changes to their type.

Example

let cars = [“Saab”“Volvo”“BMW”];
cars = 3;    // Changes array to number
const cars = [“Saab”“Volvo”“BMW”];
cars = 3;    // Not possible

Don’t Use new Object()

  • Use “” instead of new String().
  • Use 0 instead of new Number().
  • Use false instead of new Boolean().
  • Use {} instead of new Object().
  • Use [] instead of new Array().
  • Use /()/ instead of new RegExp().
  • Use function (){} instead of new Function().

Example

let x1 = “”;             // new primitive string
let x2 = 0;              // new primitive number
let x3 = false;          // new primitive boolean
const x4 = {};           // new object
const x5 = [];           // new array object
const x6 = /()/;         // new regexp object
const x7 = function(){}; // new function object

Beware of Automatic Type Conversions

JavaScript is loosely typed, meaning:

  • A variable can hold any data type.
  • A variable can change its data type during execution.

Example

let x = “Hello”;     // typeof x is a string
x = 5;               // changes typeof x to a number

Be cautious, as numbers can unintentionally be converted to strings or result in NaN (Not a Number).

During mathematical operations, JavaScript may automatically convert numbers into strings.

Example

let x = 5 + 7;       // x.valueOf() is 12,  typeof x is a number
let x = 5 + “7”;     // x.valueOf() is 57,  typeof x is a string
let x = “5” + 7;     // x.valueOf() is 57,  typeof x is a string
let x = 5 – 7;       // x.valueOf() is -2,  typeof x is a number
let x = 5 – “7”;     // x.valueOf() is -2,  typeof x is a number
let x = “5” – 7;     // x.valueOf() is -2,  typeof x is a number
let x = 5 – “x”;     // x.valueOf() is NaN, typeof x is a number

Subtracting one string from another does not produce an error, but instead returns NaN (Not a Number).

Example

“Hello” – “Dolly”    // returns NaN