Show Menu
Cheatography

JavaScript Basics Cheat Sheet by

JavaScript - Basics Concepts

Variables

let x = 10;
block scope
most used
const MAX = 100;
constant values
var y = 10;
global or function scope
old usage

Operators

let x = 10;
assignment
x == "­Hel­lo"
comparison
x === "­Hel­lo"
strict comparison
&&
logical AND
||
logical OR
!
logical negation

Data Types

let num = 10;
Number
let str = "­Hel­lo";
String
let isTrue = true
Boolean
let arr = [1, 2, 3]
Array
let obj = { a:1, b:2, c:"t­hre­e" }
Object

Array

let array = ["a",­"­b","c­"]
array definition
array.length //3
number of elements in array
array[0] // a
access first element
array[n] //0=<n­<ar­ray.length
access nth element
array[4] // error
4 grater then length of array
array[array.length - 1] // c
access last element

Comments

// this is a one line comment
/* this is a multiple 
lines comment
also called block comment */
 

Object

let object = {
   name: "Alice",
   age: 30
}
Unlike JSON, a JavaScript object does not have double quotes (") in the property name

Object usage

object.name // Alice
dot notation
object["name"] // Alice
property access notation
object.age // 30
object.gender // undefined
non-ex­istent property

try...c­at­ch...f­inally

try{
   //code might throw an error
} catch(err) {
   console.error("Error occurred", err.message);
} finally {
   console.log("Operation completed");
}
try: This block contains code that might throw an exception (an error)
catch: This block handles the exception if an error occurs in the try block
finally: This block always executes, regardless of whether an error occurs or not

Function

function greet() {
   console.log("Hello")
}
function definition

Function with parameter

function greet(name="Alice") {
   console.log("Hello")
}
parameter name with default value "­Ali­ce"
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          AngularJS Cheat Sheet
          jQuery Cheat Sheet

          More Cheat Sheets by techelopment

          JavaScript Intermediate Cheat Sheet
          JavaScript Advanced Cheat Sheet