100% found this document useful (1 vote)
20K views6 pages

JavaScript Syntax, Part I - Learn JavaScript Syntax - Introduction Cheatsheet - Codecademy

1. The document discusses JavaScript syntax concepts like variables, data types, operators, and functions. It provides examples of using console.log() to print output, declaring variables with let, const and var, and built-in functions like Math.random(). 2. Core JavaScript data types are covered including numbers, strings, Booleans, and null. Operators for arithmetic, assignment, comparison are shown. 3. Comments, template literals, and string concatenation/interpolation are also explained. Built-in global objects like Math are demonstrated.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
20K views6 pages

JavaScript Syntax, Part I - Learn JavaScript Syntax - Introduction Cheatsheet - Codecademy

1. The document discusses JavaScript syntax concepts like variables, data types, operators, and functions. It provides examples of using console.log() to print output, declaring variables with let, const and var, and built-in functions like Math.random(). 2. Core JavaScript data types are covered including numbers, strings, Booleans, and null. Operators for arithmetic, assignment, comparison are shown. 3. Comments, template literals, and string concatenation/interpolation are also explained. Built-in global objects like Math are demonstrated.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Cheatsheets / JavaScript Syntax, Part I

Learn JavaScript Syntax: Introduction


console.log()
The console.log() method is used to log or print
messages to the console. It can also be used to print console.log('Hi there!');

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 appending an instance with a period . , the // Returns a number between 0 and 1

method name, and parentheses. Math.random();

Libraries
Libraries contain methods that can be called by
appending the library name with a period . , the method Math.random();

name, and a set of parentheses. // ☝️ Math is the library

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

let price = 4.99;

String .length
The .length property of a string returns the number of
characters that make up the string. let message = 'good nite';

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 false . let lateToWork = true;

Math.random()
The Math.random() function returns a floating-point,
random number in the range from 0 (inclusive) up to but console.log(Math.random());

not including 1. // Prints: 0 - 0.9

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

// Prints: 5

Single Line Comments


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

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

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

surrounded by single quotes ' or double quotes " . let double = "Wheres my bandit hat?";
Arithmetic Operators
JavaScript supports arithmetic operators for:
// Addition

● + addition 5 + 5

● - subtraction // Subtraction

● * multiplication
10 - 5

// Multiplication

● / division
5 * 10

● % modulo // 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 good ways for a variety of The below configuration must be

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,
returns the number that remains after the right-hand // calculates # of weeks in a year, rounds
number divides into the left-hand number as many times down to nearest integer

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");
Learn Javascript: Variables
A variable is a container for data that is stored in
computer memory. It is referenced by a descriptive name // examples of variables

that a programmer can call to assign a specific value and let name = "Tammy";

retrieve it. const found = false;

var age = 3;

console.log(name, found, age);

// Tammy, false, 3

const Keyword
A constant variable can be declared using the keyword
const . It must have an assignment. Any attempt of re- const numberOfColumns = 4;

assigning a const variable will result in JavaScript numberOfColumns = 8;

runtime error. // TypeError: Assignment to constant


variable.

let Keyword
let creates a local variable in JavaScript & can be re-
assigned. Initialization during the declaration of a let let count;

variable is optional. A let variable will contain console.log(count); // Prints: undefined

undefined if nothing is assigned to it. count = 10;

console.log(count); // Prints: 10

Undefined
undefined is a primitive JavaScript value that represents
lack of defined value. Variables that are declared but not var a;

initialized to a value will have the value undefined .


console.log(a);

// Prints: undefined

Assignment Operators
An assignment operator assigns a value to its left operand
based on the value of its right operand. Here are some of let number = 100;

them:
// Both statements will add 10

● += addition assignment
number = number + 10;

● -= subtraction assignment number += 10;

● *= multiplication assignment
console.log(number);

● /= division assignment
// Prints: 120
String Concatenation
In JavaScript, multiple strings can be concatenated
together using the + operator. In the example, multiple let service = 'credit card';

strings and variables containing string values have been let month = 'May 30th';

concatenated. After execution of the code block, the let displayText = 'Your ' + service + '
displayText variable will contain the concatenated bill is due on ' + month + '.';

string.

console.log(displayText);

// Prints: Your credit card bill is due on


May 30th.

String Interpolation
String interpolation is the process of evaluating string
literals containing one or more placeholders (expressions, let age = 7;

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.`;

Template Literals
Template literals are strings that allow embedded
expressions, ${expression} . While regular strings use let name = "Codecademy";

single ' or double " quotes, template literals use console.log(`Hello, ${name}`);

backticks instead. // Prints: Hello, Codecademy

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

// Prints: Billy is 14 years old.

Variables
Variables are used whenever there’s a need to store a
piece of data. A variable contains data that can be used in const currency = '$';

the program elsewhere. Using variables also ensures code let userIncome = 85000;

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.
Declaring Variables
To declare a variable in JavaScript, any of these three
keywords can be used along with a variable name: var age;

let weight;

● var is used in pre-ES6 versions of JavaScript.


const numberOfFingers = 20;
● let is the preferred way to declare a variable
when it can be reassigned.

● const is the preferred way to declare a variable


with a constant value.

You might also like