An operator is defined to behave like a function but is restricted to specific operations on variables and values.
Some of the operators in JavaScript are arithmetic, assignment, comparison, increment, decrement and logical.
An operator is defined to behave like a function but is restricted to specific operations on variables and values.
Some of the operators in JavaScript are arithmetic, assignment, comparison, increment, decrement and logical.
JavaScript strings are surrounded by either single or double quotation marks.
String literal is the single quotation mark.
Stings
Sample Codelet str1 = “Hello World!”;
let str2 = ‘Hello World!’;
console.log(str1, str2);
Quotes can be used inside a string only if they do not match the quotes surrounding the string.
Quotes Inside Strings
Sample Codelet str1 = “It’s easy”;
let str2 = “Call me ‘Johnny'”;
let str3 = ‘Call me “Johnny”‘;
console.log(str1, str2, str3);
Assign a multiline string to a variable.
Multiline Strings Using Backticks
Sample Codelet str1 = `This is a backtick
multiple line string
spanning 3 rows.`;
console.log(str1);
Multiline Strings Using Concatenatation (+) Operator And Newline Character (\n)
Sample Codelet str2 = ‘This is a concatenated’ +
‘\nmultiple line string’ +
‘\nspanning 3 rows.’;
console.log(str2);
Multiline Strings Using Array Join
Sample Codelet lines = [‘This is an array join’, ‘multiple line string’, ‘spanning 3 rows.’];
let str3 = lines.join(‘\n’);
console.log(str3);
Return a part of the string using the slice syntax. To slice a string, specify the start index and end index separated by a comma. The first character has index 0.
Slicing
Sample Codelet str1 = “Hello, World!”;
console.log(str1.slice(2)); // Console Prints from position 3 (index 2) to the end
console.log(str1.slice(2, 5)); // Console Prints from position 3 to position 6
console.log(str1.slice(-5, -2)); // Console Prints from 6th to last character to the 3rd to last character
Table : Common Functions For Manipulating Strings
Function | Description | Example |
---|---|---|
toUpperCase() | Returns upper case | str2 = str1.toUpperCase(); |
toLowerCase() | Returns lower case | str2 = str1.toLowerCase(); |
trim() | Removes beginning or ending whitespace | str2 = str1.trim(); |
replace() | Replace a string with another | str2 = str1.replace(“Hello”, “Join”); |
split() | Split string into substrings by specified separator | str2 = str1.split(“,”); |
slice() | Returns the extracted part in a new string. | str2 = str1.slice(2); |
Strings can be combined using string concatenation via the plus “+” symbol.
Concatenate Strings
Sample Codelet str1 = “Hello”;
let str2 = “World”;
let str3 = str1 + str2;
let str4 = str1 + ” ” + str2;
console.log(str3); // Console Displays HelloWorld
console.log(str4); // Console Displays Hello World
Embed expressions and variables into strings using template strings.
Template Strings
Sample Codelet age = 24;
let name = “John”;
let combined = `My name is ${name}, and I am ${age} years old`;
console.log(combined);
Strings can be escaped using the backslash character “\”.
Table : Escape Characters
Code | Description | Example |
---|---|---|
\’ | Single Quote | str1 = ‘It\’s okay.’; |
\” | Double Quotes | str1 = “I am \”John\”.”; |
\\ | Backslash | str1 = “Insert one \\ backslash.”; |
\n | New Line | str1 = “Hello\nWorld!”; |
\r | Carriage Return | str1 = “Hello\rWorld!”; |
\t | Tab | str1 = “Hello\tWorld!”; |
\b | Backspace | str1 = “Hello \bWorld!”; |
\f | Form Feed | str1 = “Hello\fWorld!”; |
\000 | Octal value | str1 = “\110\145\154\154\157\40\127\157\162\154\144\41”; |
\xhh | Hex value | str1 = “\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21”; |
Escape Characters
Sample Codelet str1 = “My name is \”Johnny\” but you can call me \”John\”.”;
console.log(print(str1);
Booleans represent only 2 values, True or False. Any expression can be evaluated to get one of the two answers, True of False. Empty and zero values will be evaluated as False.
Boolean Values
Sample Codelet name = “John”;
let num1 = 5;
let num2 = 3;
console.log(Boolean(name)); // Console Prints true
console.log(Boolean(num1)); // Console Prints true
console.log((num1 > num2)); // Console Prints true
console.log(Boolean(false)); // Console Prints false
console.log(Boolean(0)); // Console Prints false
console.log(Boolean(“”)); // Console Prints false
console.log(Boolean([])); // Console Prints true
console.log(Boolean({})); // Console Prints true
JavaScript has 2 numeric types; integers and floats that are all stored as double precision floating point numbers.
An integer or whole number that can be positive or negative.
Integer Numbers
Sample Codelet int1 = 5;
let int2 = 3;
let int3 = -9;
console.log(int1, int2, int3); // Console Prints 5 3 -9
console.log(typeof(int1), typeof(int2), typeof(int3)); // Console Prints number number number
A float of floating point number can be positive or negative, containing one or more decimals. Scientific numbers with the letter “e” can be used to indicate the power of 10.
Float Numbers
Sample Codelet float1 = 5.0;
let float2 = 3.0;
let float3 = -9.0;
let float4 = -27.1e10;
console.log(float1, float2, float3, float4); // Console Prints 5.0 3.0 -9.0 -271000000000
console.log(typeof(float1), typeof(float2), typeof(float3), typeof(float4)); // Console Prints number number number number
Table : Common Functions For Manipulating Numbers
Function | Description | Example |
---|---|---|
toString() | Returns number as string | str1 = num1.toString(); |
toExponential() | Returns number in exponential notation | num2 = num1.toExponential(2); |
toFixed() | Returns number with fixed decimals | num2 = num1.toFixed(2); |
toPrecision() | Returns number of specific length | num2 = num1.toPrecision(2); |
valueOf() | Returns number as number | num2 = num1.valueOf(); |
Number() | Returns converted number | num2 = Number(“10”); |
parseInt() | Returns whole number | num2 = parseInt(“10”); |
parseFloat | Returns floating number | num2 = parseFloat(“10”); |
The String type is a sequence of characters inside double or single quotes.
The Number type can be a whole number or decimal number and can be either positive or negative.
The BigInt type is for storing large integers that can be beyond the safe integer limit.
The Boolean type can only be True or False.
The Undefined type is the absence of a value.
The Null type is the absence of an object.
The Symbol type creates unique keys.
Objects are mutable values equivalent to key value pairs such as arrays.
You can get the data type of any object by using the built-in typeof function.
Data Type
Sample Codemy_string = “Hello world!”;
alert(my_string) // Dialog Prints Hello world!
alert(typeof(my_string)) // Dialog Prints string
Casting allows the variable type to be changed for the
Number
,
String
and
Boolean and Object Date
data types.
Casting Integers
Sample Codelet int1 = Number(“5”); // Will be 5
let int2 = parseInt(“3.0”); // Will be 3
let int3 = Number(“9”); // Will be 9
Casting Floats
Sample Codelet float1 = Number(“5.0”); // Will be 5.0
let float2 = parseFloat(“3.0”); // Will be 3.0
let float3 = Number(“9.0”); // Will be 9.0
Casting Strings
Sample Codelet str1 = String(“s5”); // Will be ‘s5’
let str2 = String(3) // Will be ‘3’
let str3 = String(9.0) // Will be ‘9.0’
A data type is a grouping of data values. Variables are created to store a data type. In JavaScript, the assigning of a value will indicate the data type. JavaScript does not require a specific format in order to display the value.
JavaScript has dynamic types allowing the same variable to hold different data types.
Table : Data Types
Type | Description | Example |
---|---|---|
String | Stores a sequence of characters | name = “Jake”; |
Number | Stores an integer or float | age = 24; |
BigInt | Integer with arbitrary precision | amount = 10000000000n; |
Boolean | Stores either true or false | is_available = True; |
Undefined | Variable not assigned a value | let job; |
Null | Absence of an object value | let empty = null; |
Symbol | Unique and immutable value | let symbol = Symbol(“unique”); |
Object | Collection of key value pairs | person = {“name”: “Jake”, “age”: 24}; |
typeof | Returns data type | result = typeof(“name”) |
Variables store information such as values or other variables.
JavaScript variables do not need to be declared with any type and the type can be changed after been set.
Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.
Variable names are case-sensitive.
A variable name cannot be any of the JavaScript keywords.
Variable Names
Sample Codevar name = ‘John’; // name is type str
let age = 18; // age is type int
const height = 5.3; // height is type float
var myVarName = ‘Johnny’; // Camel Case is where each word, except the first, starts with a capital letter
var MyVarName = ‘Johnny’; // Pascal Case is where each word starts with a capital letter
var my_var_name = ‘Johnny’; // Snake Case is where each word is separated by an underscore
var car = fruit = color = ‘red’; // Assign the same value to multiple variables on one line
Output Variables
Sample Codealert(name); // Dialog outputs a single variable
console.log(name); // Console outputs a single variable
alert(name +” “+ age + ” “+ height); // Dialog outputs multiple variables, separated by a space
console.log(name, age, height); // Console outputs multiple variables, separated by a comma
alert(name + myVarName + car) // Dialog outputs multiple non numeric variables, concatenated by a plus symbol
console.log(name + myVarName + car) // Console outputs multiple non numeric variables, concatenated by a plus symbol
The sequence /* is used to declare the start of the comment block and the sequence */ is used to
declare the end of comment. All text between the start and end sequences is interpreted as a
comment, even if the text is otherwise valid JavaScript syntax. These are sometimes called “C-style” comments.
Multiline Comments
Sample CodeThis is a comment
that goes on
multiple lines
*/
Double forward slashes // will mark all text until a newline as a comment.
Single Line Comment Example
Sample Code// This is a single line comment
JavaScript has a simple syntax and grammar similar to the C programming language. It uses curly brackets to delimit blocks, and semicolons after statements.
Variables can be defined using the var keyword. Variables can also be defined using const for constant variables and let for local variables. Constant variables cannot be re-declared or reassigned.
Hello World Example
Sample Codealert(‘Hello world’); //Dialog Prints Hello world
console.log(‘Hello world’); //Console Prints Hello world