JavaScript Operators
operators
• Examples:
• The Assignment Operator = assigns values
• The Addition Operator + adds values
• The Multiplication Operator * multiplies
values
• The Comparison Operator > compares values
JavaScript Assignment
JavaScript Addition
The Addition Operator (+) adds numbers:
JavaScript Multiplication
The Multiplication Operator (*) multiplies numbers:
JavaScript Arithmetic Operators
JavaScript Arithmetic Operators
JavaScript Assignment Operators
The Addition Assignment Operator (+=) adds a value to a variable.
JavaScript Comparison Operators
JavaScript Comparison Operators
JavaScript String Addition
The + can also be used to add (concatenate) strings:
Adding Strings and Numbers
JavaScript Logical Operators
JavaScript Type Operators
JavaScript Bitwise Operators
Bit operators work on 32 bits numbers.
Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript
number.
JavaScript Data Types
• JavaScript has 8 Datatypes
• String
Number
Bigint
Boolean
Undefined
Null
Symbol
Object
The Object Datatype
• The object data type can contain both built-in
objects, and user defined objects:
Built-in object types can be:
objects, arrays, dates, maps, sets, intarrays,
floatarrays, promises, and more.
Examples
• // Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Boolean
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
When adding a number and a string,
JavaScript will treat the number as a string.
JavaScript Types are Dynamic
JavaScript Functions
• A JavaScript function is a block of code
designed to perform a particular task.
• A JavaScript function is executed when
"something" invokes it (calls it).
Example
JavaScript Function Syntax
• A JavaScript function is defined with
the function keyword, followed by a name, followed
by parentheses ().
• Function names can contain letters, digits,
underscores, and dollar signs (same rules as variables).
• The parentheses may include parameter names
separated by commas:
(parameter1, parameter2, ...)
• The code to be executed, by the function, is placed
inside curly brackets: {}
• function name(parameter1, parameter2,
parameter3) {
// code to be executed
}
Function Invocation
• The code inside the function will execute when
"something" invokes (calls) the function:
• When an event occurs (when a user clicks a button)
• When it is invoked (called) from JavaScript code
• Automatically (self invoked)
Function Return
• When JavaScript reaches a return statement,
the function will stop executing.
• If the function was invoked(calls) from a
statement, JavaScript will "return" to execute
the code after the invoking statement.
• Functions often compute a return value.
• The return value is "returned" back to the
"caller":
Example-
Calculate the product of two numbers, and return the result: