Ali The Expert

Logo

JavaScript Cheat Sheet that you should bookmark

JavaScript Cheat Sheet that you should bookmark

This is the Cheat Sheet of JavaScript. It contains explanations, code examples, basic and important operators, functions, principles, methods, and many more. It provides a good overview of the language.

Javascript

JavaScript is a programming language that powers the dynamic behavior on most websites. Alongside HTML and CSS, it is a core technology that makes the web run.

console.log()

The console.log() method is used to log or print messages to the console. It can also be used to print objects and other info.
console.log("Hi there!");
// Prints: Hi there!

Strings

Strings are a primitive data type. They are any grouping of characters (letters, spaces, numbers, or symbols) surrounded by single quotes ‘ or double quotes ” .
let single = 'Wheres my bandit hat?';
let double = "Wheres my bandit hat?";

Numbers

Numbers are a primitive data type. They include the set of all integers and floating point numbers.
let amount = 6;
let price = 4.99;

Boolean

Booleans are a primitive data type. They can be either true or false .
let lateToWork = true;
let isHoliday = false;

Null

Null is a primitive data type. It represents the intentional absence of value. In code, it is represented as null.
let x = null;

Arithmetic Operators

JavaScript supports arithmetic operators for:

  • – addition
  • – subtraction
  • multiplication
  • / division
  • % modulo
// Addition
5 + 5;
// Subtraction
10 - 5;
// Multiplication
5 * 10;
// Division
10 / 5;
// Modulo
10 % 5;

String.length

The .length property of a string returns the number of characters that make up the string.
let message = "good nite";
console.log(message.length);
// Prints: 9
console.log("howdy".length);
// Prints: 5

Methods

Methods return information about an object, and are called by appending an instance with a period . , the method name, and parentheses.

Libraries

Libraries contain methods that can be called by appending the library name with a period . , the method name, and a set of parentheses.

Math.random()

The Math.random() function returns a floating-point random number in the range from 0 (inclusive) up to but not including 1.
// Returns a number between 0 and 1
Math.random();

Math.floor()

The Math.floor() function returns the largest integer less than or equal to the given number.
console.log(Math.floor(5.95));
// Prints: 5

Single Line Comments

In JavaScript, single-line comments are created with two consecutive forward slashes // .
// This line will denote a comment

Multi-line Comments

In JavaScript, multi-line comments are created by surrounding the lines with /* at the beginning and */ at the end. Comments are good ways for a variety of reasons like explaining a code block or indicating some hints, etc.
/*
The below configuration must be
changed before deployment.
*/
let baseUrl = "https://google.com/";

Variables

A variable is a container for data that is stored in computer memory. It is referenced by a descriptive name that a programmer can call to assign a specific value and retrieve it.
// examples of variables
let name = "Tammy";
const found = false;
var age = 3;
console.log(name, found, age);
// Tammy, false, 3