0% found this document useful (0 votes)
3K views5 pages

Codecademy JavaScript Basics Guide

The document provides an introduction to key JavaScript concepts like variables, data types, operators, and functions. It explains that JavaScript is used to add dynamic behavior to websites and works alongside HTML and CSS. It also defines common terms like variables, strings, numbers, booleans, and functions.

Uploaded by

ken jay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views5 pages

Codecademy JavaScript Basics Guide

The document provides an introduction to key JavaScript concepts like variables, data types, operators, and functions. It explains that JavaScript is used to add dynamic behavior to websites and works alongside HTML and CSS. It also defines common terms like variables, strings, numbers, booleans, and functions.

Uploaded by

ken jay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cheatsheets / Learn JavaScript

Introduction

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

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 // Returns a number between 0 and 1
called by appending an instance with a period . , the
[Link]();
method name, and parentheses.

Libraries
Libraries contain methods that can be called by [Link]();
appending the library name with a period . , the method
name, and a set of parentheses.
// ☝️ Math is the library

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 of let message = 'good nite';
characters that make up the string.
[Link]([Link]);
// Prints: 9

[Link]('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 let lateToWork = true;
true or false .

[Link]()
The [Link]() function returns a floating-point, [Link]([Link]());
random number in the range from 0 (inclusive) up to but
// Prints: 0 - 0.9
not including 1.

[Link]()
The [Link]() function returns the largest integer [Link]([Link](5.95));
less than or equal to the given number.
// Prints: 5

Single Line Comments


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

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

Strings
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
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 below configuration must be
the end. Comments are good ways for a variety of
reasons like explaining a code block or indicating some changed before deployment.
hints, etc. */

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 = [Link](365/7);

// calcuates the number of days left over


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

[Link]("A year has " + weeksInYear +


" weeks and " + daysLeftOver + " days");

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

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

// 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. [Link](currency + userIncome + ' is
more than the average income.');
// Prints: $85000 is more than the average
income.

Undefined
undefined is a primitive JavaScript value that var a;
represents lack of defined value. Variables that are
declared but not initialized to a value will have the value
undefined . [Link](a);
// Prints: undefined

Learn Javascript: Variables


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

Declaring Variables
To declare a variable in JavaScript, any of these three var age;
keywords can be 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 const numberOfFingers = 20;
when it can be reassigned.
const is the preferred way to declare a variable
with a constant value.

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

[Link](`Billy is ${6+8} years old.`);


// Prints: Billy is 14 years old.

let Keyword
let creates a local variable in JavaScript & can be re- let count;
assigned. Initialization during the declaration of a let
[Link](count); // Prints: undefined
variable is optional. A let variable will contain
undefined if nothing is assigned to it. count = 10;
[Link](count); // Prints: 10

const Keyword
A constant variable can be declared using the keyword const numberOfColumns = 4;
const . It must have an assignment. Any attempt of re-
numberOfColumns = 8;
assigning a const variable will result in JavaScript
runtime error. // TypeError: Assignment to constant
variable.

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

[Link](displayText);
// Prints: Your credit card bill is due on
May 30th.

Save Print Share

Common questions

Powered by AI

Primitive data types in JavaScript represent the simplest form of data and include numbers, strings, booleans, null, and undefined. These data types are immutable, meaning once a value is assigned, it cannot be changed. Numbers include integers and floating-point values, strings are sequences of characters, booleans represent true or false, null is the intentional absence of value, and undefined signifies a declared variable that hasn't been assigned a value .

JavaScript's arithmetic operators facilitate basic numerical operations: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). These operators allow for numeric calculations in programs. For instance, the expression 5 + 10 performs addition to result in 15, while 10 % 3 returns 1, representing the remainder of division .

In JavaScript, 'null' is explicitly assigned to represent no value and is used when a developer wants to indicate the intentional absence of an object value. In contrast, 'undefined' occurs when a variable is declared but not initialized. 'null' might be used as a placeholder in objects, while 'undefined' typically indicates that a variable has yet to be assigned a value .

JavaScript handles random number generation through the Math.random() function, which returns a floating-point number between 0 (inclusive) and 1 (exclusive). Additionally, Math.floor() can be used to obtain whole numbers by rounding down. For instance, Math.floor(5.95) results in 5 .

String interpolation in JavaScript, performed using template literals marked by backticks (`), allows embedding expressions within strings using the ${expression} syntax. This method makes code more readable and maintainable than traditional string concatenation, which requires multiple + operators. For example, `Hello, ${name}` is more straightforward than 'Hello, ' + name .

Comments enhance JavaScript code by providing explanations or annotations, making it easier to understand. Single-line comments use two forward slashes (//), while multi-line comments are enclosed within /* and */. These annotations do not affect the execution of code but serve as documentation for developers .

The console.log() method is vital for debugging and displaying output in JavaScript. It is used to log or print messages to the console, making it easier for developers to track the flow of a program and understand the state of variables during execution. This method can also print objects and other info to aid in debugging .

JavaScript variables contribute to code re-usability and readability by storing data that can be accessed or modified throughout a program. Declaring variables using var, let, or const helps ensure clear, maintainable code. 'var' is used in older JavaScript versions, 'let' allows reassignment of variables, and 'const' is used for variables that should not change. For instance, let name = "Tammy"; or const numberOfFingers = 20 ensures specific uses .

The assignment operator in JavaScript assigns a value to its left operand based on the value of its right operand. Examples include basic assignment (=), addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), and division assignment (/=). For instance, if let number = 100; using number += 10 increases number by 10, resulting in 110 .

Template literals improve JavaScript code by allowing more intuitive syntax for string operations and multi-line strings without concatenation. Unlike regular strings, which require + for concatenation and '\n' for new lines, template literals use backticks to embed expressions and maintain cleaner, more readable code. For example, `User has ${num} notifications` is more straightforward than 'User has ' + num + ' notifications' .

You might also like