Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19K views

Learn JavaScript - Introduction Cheatsheet - Codecademy

1) JavaScript allows logging messages to the console using console.log() and printing objects and other info. It is a core web technology alongside HTML and CSS. 2) Methods return information about objects and are called using dot notation, while built-in objects contain methods that can be accessed the same way. 3) Variables are containers that store and reference data in code through meaningful names and can be declared using let, var, or const.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19K views

Learn JavaScript - Introduction Cheatsheet - Codecademy

1) JavaScript allows logging messages to the console using console.log() and printing objects and other info. It is a core web technology alongside HTML and CSS. 2) Methods return information about objects and are called using dot notation, while built-in objects contain methods that can be accessed the same way. 3) Variables are containers that store and reference data in code through meaningful names and can be declared using let, var, or const.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Cheatsheets / Learn JavaScript

Introduction

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

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.

Methods
Methods return information about an object, and are called by // Returns a number between 0 and 1
appending an instance with a period . , the method name, and
Math.random();
parentheses.

Built-in Objects
Built-in objects contain methods that can be called by appending the Math.random();
object name with a period . , the method name, and a set of
parentheses.
// ☝️ Math is the built-in object

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

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

console.log('howdy'.length);
// Prints: 5

Data Instances
When a new piece of data is introduced into a JavaScript program, the
program keeps track of it in an instance of that data type. An instance
is an individual case of a data type.
Booleans
Booleans are a primitive data type. They can be either true or let lateToWork = true;
false .

Math.random()
The Math.random() method returns a floating-point, random console.log(Math.random());
number in the range from 0 (inclusive) up to but not including 1.
// Prints: 0 - 0.9999999999999999

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

Single Line Comments


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

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

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

Arithmetic Operators
JavaScript supports arithmetic operators for: // Addition
+ addition
5 + 5
- subtraction
* multiplication // Subtraction
/ division 10 - 5
% modulo // Multiplication
5 * 10
// Division
10 / 5
// Modulo
10 % 5
Multi-line Comments
In JavaScript, multi-line comments are created by surrounding the /*
lines with /* at the beginning and */ at the end. Comments are
The below configuration must be
good ways for a variety of reasons like explaining a code block or
indicating some hints, etc. changed before deployment.
*/

let baseUrl = 'localhost/taxwebapp/country';

Remainder / Modulo Operator


The remainder operator, sometimes called modulo, returns the // calculates # of weeks in a year, rounds down to
number that remains after the right-hand number divides into the
nearest integer
left-hand number as many times as it evenly can.
const weeksInYear = Math.floor(365/7);

// calcuates the number of days left over after


365 is divded by 7
const daysLeftOver = 365 % 7 ;

console.log("A year has " + weeksInYear + " weeks


and " + daysLeftOver + " days");

Assignment Operators
An assignment operator assigns a value to its left operand based on let number = 100;
the value of its right operand. Here are some of them:
+= addition assignment
-= subtraction assignment // Both statements will add 10
*= multiplication assignment number = number + 10;
/= division assignment number += 10;

console.log(number);
// Prints: 120

String Interpolation
String interpolation is the process of evaluating string literals let age = 7;
containing one or more placeholders (expressions, variables, etc).
It can be performed using template literals: text ${expression} text .
// String concatenation
'Tommy is ' + age + ' years old.';

// String interpolation
`Tommy is ${age} years old.`;
Variables
Variables are used whenever there’s a need to store a piece of data. A const currency = '$';
variable contains data that can be used in the program elsewhere.
let userIncome = 85000;
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
undefined is a primitive JavaScript value that represents lack of var a;
defined value. Variables that are declared but not initialized to a value
will have the value undefined .
console.log(a);
// Prints: undefined

Learn Javascript: Variables


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

Declaring Variables
To declare a variable in JavaScript, any of these three keywords can be var age;
used along with a variable name:
let weight;
var is used in pre-ES6 versions of JavaScript.
let is the preferred way to declare a variable when it can be const numberOfFingers = 20;
reassigned.
const is the preferred way to declare a variable with a
constant value.

Template Literals
Template literals are strings that allow embedded expressions, let name = "Codecademy";
${expression} . While regular strings use single ' or double "
console.log(`Hello, ${name}`);
quotes, template literals use backticks instead.
// Prints: Hello, Codecademy

console.log(`Billy is ${6+8} years old.`);


// Prints: Billy is 14 years old.

let Keyword
let creates a local variable in JavaScript & can be re-assigned. let count;
Initialization during the declaration of a let variable is optional. A let
console.log(count); // Prints: undefined
variable will contain undefined if nothing is assigned to it.
count = 10;
console.log(count); // Prints: 10
const Keyword
A constant variable can be declared using the keyword const . It must const numberOfColumns = 4;
have an assignment. Any attempt of re-assigning a const variable will
numberOfColumns = 8;
result in JavaScript runtime error.
// TypeError: Assignment to constant variable.

String Concatenation
In JavaScript, multiple strings can be concatenated together using the let service = 'credit card';
+ operator. In the example, multiple strings and variables containing
let month = 'May 30th';
string values have been concatenated. After execution of the code
block, the displayText variable will contain the concatenated string. let displayText = 'Your ' + service + ' bill is
due on ' + month + '.';

console.log(displayText);
// Prints: Your credit card bill is due on May
30th.

Print Share

You might also like