Learn JavaScript - Introduction Cheatsheet - Codecademy
Learn JavaScript - Introduction Cheatsheet - Codecademy
Introduction
Assignment Operators
An assignment operator assigns a value to its left operand let number = 100;
based on the value of its right operand. Here are some of
them:
+= addition assignment // Both statements will add 10
-= subtraction assignment number = number + 10;
*= multiplication assignment
number += 10;
/= division assignment
console.log(number);
// Prints: 120
String Interpolation
// String interpolation
`Tommy is ${age} years old.`;
Variables
Variables are used whenever there’s a need to store a const currency = '$';
piece of data. A variable contains data that can be used in
let userIncome = 85000;
the program elsewhere. Using variables also ensures code
re-usability since it can be used to replace the same
value in multiple places. console.log(currency + userIncome + ' is
more than the average income.');
// Prints: $85000 is more than the average
income.
Undefined
Declaring Variables
Template Literals
Template literals are strings that allow embedded let name = "Codecademy";
expressions, ${expression} . While regular strings
console.log(`Hello, ${name}`);
use single ' or double " quotes, template literals use
backticks instead. // Prints: Hello, Codecademy
let creates a local variable in JavaScript & can be re- let count;
assigned. Initialization during the declaration of a let
console.log(count); // Prints: undefined
variable is optional. A let variable will contain
undefined if nothing is assigned to it. count = 10;
console.log(count); // Prints: 10
const Keyword
String Concatenation
console.log(displayText);
// Prints: Your credit card bill is due on
May 30th.
console.log()
Methods
Methods return information about an object, and are // Returns a number between 0 and 1
called by appending an instance with a period . , the
Math.random();
method name, and parentheses.
Built-in Objects
Numbers
Numbers are a primitive data type. They include the set let amount = 6;
of all integers and floating point numbers.
let price = 4.99;
String .length
The .length property of a string returns the number let message = 'good nite';
of characters that make up the string.
console.log(message.length);
// Prints: 9
console.log('howdy'.length);
// Prints: 5
Data Instances
Booleans
Booleans are a primitive data type. They can be either let lateToWork = true;
true or false .
Math.random()
Math.floor()
In JavaScript, single-line comments are created with two // This line will denote a comment
consecutive forward slashes // .
Null
Strings are a primitive data type. They are any grouping of let single = 'Wheres my bandit hat?';
characters (letters, spaces, numbers, or symbols)
let double = "Wheres my bandit hat?";
surrounded by single quotes ' or double quotes " .
Arithmetic Operators
Multi-line Comments
let baseUrl =
'localhost/taxwebapp/country';
Remainder / Modulo Operator
The remainder operator, sometimes called modulo, // calculates # of weeks in a year, rounds
returns the number that remains after the right-hand
down to nearest integer
number divides into the left-hand number as many times
as it evenly can. const weeksInYear = Math.floor(365/7);
Print Share