Ca Js Intro
Ca Js Intro
Introduction
console.log()
The console.log() method is used to log or print console.log('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
Math.random();
method name, and parentheses.
Built-in Objects
Built-in objects contain methods that can be called by Math.random();
appending the 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 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
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 1/6
10/10/23, 16.28 Learn JavaScript: Introduction Cheatsheet | Codecademy
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 .
Math.random()
The Math.random() method returns a floating-point, console.log(Math.random());
random number in the range from 0 (inclusive) up to
// Prints: 0 - 0.9999999999999999
but not including 1.
Math.floor()
The Math.floor() function returns the largest integer console.log(Math.floor(5.95));
less than or equal to the given number.
// Prints: 5
Null
Null is a primitive data type. It represents the let x = null;
intentional absence of value. In code, it is represented
as null .
Strings
Strings are a primitive data type. They are any grouping let single = 'Wheres my bandit hat?';
of characters (letters, spaces, numbers, or symbols)
let double = "Wheres my bandit hat?";
surrounded by single quotes ' or double quotes " .
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 2/6
10/10/23, 16.28 Learn JavaScript: Introduction Cheatsheet | Codecademy
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 */
The below configuration must be
at 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';
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 3/6
10/10/23, 16.28 Learn JavaScript: Introduction Cheatsheet | Codecademy
Assignment Operators
An assignment operator assigns a value to its left let number = 100;
operand 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 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
let userIncome = 85000;
in 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
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 . console.log(a);
// Prints: undefined
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 4/6
10/10/23, 16.28 Learn JavaScript: Introduction Cheatsheet | Codecademy
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
console.log(`Hello, ${name}`);
single ' or double " quotes, template literals use
backticks instead. // Prints: Hello, Codecademy
let Keyword
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
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.
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 5/6
10/10/23, 16.28 Learn JavaScript: Introduction Cheatsheet | Codecademy
String Concatenation
In JavaScript, multiple strings can be concatenated let service = 'credit card';
together using the + operator. In the example,
let month = 'May 30th';
multiple strings and variables containing string values
have been concatenated. After execution of the code let displayText = 'Your ' + service + '
block, the displayText variable will contain the bill is due on ' + month + '.';
concatenated string.
console.log(displayText);
// Prints: Your credit card bill is due
on May 30th.
Print Share
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-introduction/cheatsheet 6/6