Category Archives: Java Script

JavaScript Reserved Words

In JavaScript you cannot use these reserved words as variables, labels, or function names: abstract arguments await* boolean break byte case catch char class* const continue debugger default delete do double else enum* eval export* extends* false final finally float for function goto if implements import* in instanceof int interface let* long native new null… Read More »

JavaScript Performance

How to speed up your JavaScript code. Reduce Activity in Loops Loops are often used in programming. Each statement in a loop, including the for statement, is executed for each iteration of the loop. Statements or assignments that can be placed outside the loop will make the loop run faster. Bad: for (let i = 0; i <… Read More »

JavaScript Common Mistakes

This chapter points out some common JavaScript mistakes. Accidentally Using the Assignment Operator JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (==) in an if statement. This if statement returns false (as expected) because x is not equal to 10: Example: <!DOCTYPE html> <html> <body> <h2>JavaScript Comparisons</h2>… Read More »

JavaScript Best Practices

Avoid global variables, avoid new, avoid ==, avoid eval() Avoid Global Variables Minimize the use of global variables. This includes all data types, objects, and functions. Global variables and functions can be overwritten by other scripts. Use local variables instead, and learn how to use closures. Always Declare Local Variables All variables used in a function should be declared… Read More »

JavaScript Style Guide

Always use the same coding conventions for all your JavaScript projects. JavaScript Coding Conventions Coding conventions are style guidelines for programming. They typically cover: Naming and declaration rules for variables and functions. Rules for the use of white space, indentation, and comments. Programming practices and principles Coding conventions secure quality: Improves code readability Make code maintenance easier… Read More »

JavaScript Debugging

Errors can (will) happen, every time you write some new computer code. Code Debugging Programming code might contain syntax errors, or logical errors. Many of these errors are difficult to diagnose. Often, when programming code contains errors, nothing will happen. There are no error messages, and you will get no indications where to search for… Read More »

JavaScript JSON

JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page. What is JSON? JSON stands for JavaScript Object Notation JSON is a lightweight data interchange format JSON is language independent * JSON is “self-describing” and easy to understand * The JSON syntax is derived from… Read More »

JavaScript Modules

Modules JavaScript modules allow you to break up your code into separate files. This makes it easier to maintain the code-base. JavaScript modules rely on the import and export statements. Export You can export a function or variable from any file. Let us create a file named person.js, and fill it with the things we want to export. There are… Read More »

JavaScript Classes

ECMAScript 2015, also known as ES6, introduced JavaScript Classes. JavaScript Classes are templates for JavaScript Objects. JavaScript Class Syntax Use the keyword class to create a class. Always add a method named constructor(): Syntax class ClassName {  constructor() { … }} Example class Car {  constructor(name, year) {    this.name = name;    this.year = year;  }} The example above creates a class named “Car”. The class… Read More »

JavaScript Arrow Function

Arrow functions were introduced in ES6. Arrow functions allow us to write shorter function syntax: Example: <!DOCTYPE html> <html> <body> <h2>JavaScript Arrow Function</h2> <p>This example shows the syntax of an Arrow Function, and how to use it.</p> <p id=”demo”></p> <script> let myFunction = (a, b) => a * b; document.getElementById(“demo”).innerHTML = myFunction(4, 5); </script> </body>… Read More »