Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript Strings

A string, also known as a text string, comprises a sequence of characters, such as “John Doe”.

Strings are enclosed within quotes, which can be either single or double quotes.

Example

// Using double quotes:
let carName1 = “Volvo XC60”;

// Using single quotes:
let carName2 = ‘Volvo XC60’;

You can include quotes within a string, as long as they do not match the quotes used to delimit the string.

Example

// Single quote inside double quotes:
let answer1 = “It’s alright”;

// Single quotes inside double quotes:
let answer2 = “He is called ‘Johnny'”;

// Double quotes inside single quotes:
let answer3 = ‘He is called “Johnny”‘;

JavaScript Numbers

All numbers in JavaScript are stored as decimal numbers, represented in floating-point format.

Numbers can be expressed with or without decimals.

Example

// With decimals:
let x1 = 34.00;

// Without decimals:
let x2 = 34

Exponential Notation

Very large or very small numbers can be represented using scientific (exponential) notation in JavaScript.

Example

let y = 123e5;    // 12300000
let z = 123e-5;   // 0.00123

While many programming languages provide a range of numeric types, encompassing integers and floating-point numbers of different sizes.

JavaScript distinguishes itself by offering just one numeric type: double, specifically a 64-bit floating-point number.

JavaScript BigInt

In JavaScript, all numbers are stored using a 64-bit floating-point format.

Introduced in ES2020, JavaScript BigInt is a new data type designed to store integer values that exceed the capacity of a standard JavaScript Number.

Example

let x = BigInt(“123456789012345678901234567890”);

JavaScript Booleans

Booleans can only hold two possible values: true or false.

Example

let x = 5;
let y = 5;
let z = 6;
(x == y)       // Returns true
(x == z)       // Returns false 

Booleans find frequent application in conditional testing.

JavaScript Arrays

JavaScript arrays are denoted by square brackets and individual elements are separated by commas.

The code below initializes an array named cars, comprising three items, each representing a car name:

Example

const cars = [“Saab”“Volvo”“BMW”];

Array indexes in JavaScript adhere to a zero-based system, signifying that the initial item is accessed using [0], the second with [1], and so forth.

JavaScript Objects

JavaScript objects are denoted by curly braces {}.

Properties within objects are expressed as name:value pairs, each separated by commas.

Example

const person = {firstName:“John”, lastName:“Doe”, age:50, eyeColor:“blue”};

The object, referred to as “person” in the provided example, encompasses four properties: firstName, lastName, age, and eyeColor.

The typeof Operator

The typeof operator in JavaScript allows you to determine the type of a JavaScript variable.

By using the typeof operator, you can obtain the type of a variable or an expression.

Example

typeof “”             // Returns “string”
typeof “John”         // Returns “string”
typeof “John Doe”     // Returns “string”

Example

typeof 0              // Returns “number”
typeof 314            // Returns “number”
typeof 3.14           // Returns “number”
typeof (3)            // Returns “number”
typeof (3 + 4)        // Returns “number”

Undefined

In JavaScript, when a variable lacks a value, it holds the value undefined, and its type is also undefined.

Example

let car;    // Value is undefined, type is undefined
 
Setting a variable’s value to undefined effectively empties it, resulting in both the value and the type becoming undefined.

Example

car = undefined;    // Value is undefined, type is undefined

Empty Values

An empty value is distinct from undefined.

An empty string possesses a valid value and a defined type.

Example

let car = “”;    // The value is “”, the typeof is “string”