JavaScript
JavaScript is a scripting language for the web.
It was developed to introduce interactivity into
HTML pages.
As it builds interactive webpages, so it is an
essential part of the web application
It is a dynamic and interpreted programming
language.
It is loosely typed.
It runs in the JavaScript engine in the browser.
JavaScript engines were originally used only in web
browsers, but they are now core components of
other software systems, most notably servers and a
variety of applications.
JavaScript variables
JavaScript includes variables which hold the
data value and it can be changed anytime.
JavaScript uses reserved keyword var to
declare a variable.
A variable must have a unique name.
var Name
Var Name = “LPU”
JavaScript variables
JavaScript variables are loosely typed
Diff among var, let and const
Dynamic typing
JavaScript is a loosely typed and dynamic
language. Variables in JavaScript are not
directly associated with any particular value
type, and any variable can be assigned (and
re-assigned) values of all types:
let foo = 42; // foo is now a number
foo = 'bar'; // foo is now a string
foo = true; // foo is now a boolean
Primitive data types
All types except objects define immutable
values (that is, values which can't be
changed).
Strings are immutable. We refer to values of
these types as "primitive values".
String type
JavaScript's String type is used to represent
textual data.
Primitive data types
Number type
ECMAScript has two built-in numeric types:
Number and BigInt.
The Number type is a double-precision 64-bit
binary format IEEE 754 value (numbers
between -(2^53 − 1) and 2^53 − 1).
Primitive data types
BigInt
A BigInt value, also sometimes just called a
BigInt, is a bigint primitive, created by
appending n to the end of an integer literal, or
by calling the BigInt() function (without the new
operator) and giving it an integer value or string
value.
Primitive data types
//JS defines the maximum safe interger as a constant
Number.MAX_SAFE_INTEGR
const safeInt = Number.MAX_SAFE_INTEGER // ->
9_007_199_254_740_991
// If we add one we get
safeInt + 1 // -> 9_007_199_254_740_992
// If we add 2...
safeInt + 2 // -> 9_007_199_254_740_992
// Therefore 9_007_199_254_740_992 or (2^53) is deemed unsafe
because two real world numbers 9_007_199_254_740_992 and
9_007_199_254_740_993 are represented through it. That is why
safeInt + 1 === safeInt + 2 // -> true
Primitive data types
// Strings
BigInt("1111111111111111111111111111111111111")
// -> 1111111111111111111111111111111111111n
// Binary
BigInt(0b100000000000000000000000000000000000000000000000000
000000000000000000001111111)
// -> 151115727451828646838272n
// Hexadecimal
BigInt(0xfffffffffffffffffffffffffffffffffff9fff9fffffffffffffffff)
// -> 95780971304118053647396689196894323976171195136475136n
// Octal
BigInt(0o40000000000000000000000000000000000000000011112444)
// -> 713623846352979940529142984724747568191373312n
Primitive data types
Boolean type
Boolean represents a logical entity and can
have two values: true and false.
Null type
The Null type has exactly one value: null.
Undefined type
A variable that has not been assigned a value
has the value undefined.
Primitive data types
Symbol
Symbol is a built-in object whose constructor
returns a symbol primitive — also called a
Symbol value or just a Symbol — that’s
guaranteed to be unique.
Symbols are often used to add unique property
keys to an object that won’t collide with keys
any other code might add to the object, and
which are hidden from any mechanisms other
code will typically use to access the object.
Reference data types
Objects
An object is a standalone entity, with properties
and type. It stores related information of an entity.
In JavaScript, objects can be seen as a collection of
properties.
Arrays
Array is a single variable that is used to store
different elements. Unlike most languages where
array is a reference to the multiple variable.
Functions
A set of statements that performs a task or
calculates a value.