JavaScript Tutorial 02
JavaScript Tutorial 02
1.Primitives
The latest ECMAScript standard defines eight data types: Seven data types that
are primitives:
console.log("Hello", firstName);
console.log("You are", age, "years old");
console.log("Enrolled:", student);
Arrays
Arrays can hold more than one variable and special types of object. Arrays consist of
square brackets and items that are separated by commas.
We use arrays whenever we want to create and store a list of multiple items in a single
variable. Arrays are especially useful when creating ordered collections where items in
the collection can be accessed by their numerical position in the list
Syntax:
Declaring a variable as const only means that you cannot assign a new value to that
variable once a value has been assigned. you can still perform operations on the array
elements, such as adding new elements to the array, changing the value of an element,
or removing elements.
<p id="demo"></p> <p id="demo"></p>
<script> <script>
// Create an Array: // Create an Array:
cars = ["Saab", "Volvo", "BMW"]; const cars = ["Saab", "Volvo", "BMW"];
//Reassign array //Reassign array
cars = ["SUV", "Sport", "Muscle"]; cars = ["SUV", "Sport", "Muscle"];
1
COM 2303, ICT 2204
JavaScript arrays can contain any and all types of data at the
same time!
In the above example, each item is a string, but in an array we can store various data
types — strings, numbers, objects, and even other arrays. We can also mix data types in
a single array — we do not have to limit ourselves to storing only numbers in one array,
and in another only strings. For example:
Objects
Objects are used to represent a “thing” in your code. That could be a person, a car, a
building, a book, a character in a game — basically anything that is made up or can be
defined by a set of characteristics
<p id="demo"></p>
<script>
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
2
COM 2303, ICT 2204
An object is made up of multiple members, each of which has a name, and a value. Each
name/value pair must be separated by a comma, and the name and value in each case
are separated by a colon
JavaScript is a dynamically typed language. This means you don't have to specify the
data type of a variable when you declare it. It also means that data types are
automatically converted as-needed during script execution.
And later, you could assign the same variable a string value, for example:
Because JavaScript is dynamically typed, this assignment does not cause an error
message.
3
COM 2303, ICT 2204
console.log(typeof age);
age = Number(age);
age += 1;
/*
let x;
let y;
let z;
x = Number("pizza");
y = String(3.14);
z = Boolean("pizza");
4
COM 2303, ICT 2204
Flow Control
Conditional Statements
if...else statement
if (condition) {
statement1;
} else {
statement2;
}
5
COM 2303, ICT 2204
Here, the condition can be any expression that evaluates to true or false.
If condition evaluates to true, statement_1 is executed. Otherwise, statement_2 is
executed. statement_1 and statement_2 can be any statement, including further nested
if statements.
You can also compound the statements using else if to have multiple conditions tested
in sequence, as follows:
if (condition1) {
statement1;
} else if (condition2) {
statement2;
} else if (conditionN) {
statementN;
} else {
statementLast;
}
In the case of multiple conditions, only the first logical condition which evaluates to true
will be executed. To execute multiple statements, group them within a block statement
({ /* … */ }).
In general, it's good practice to always use block statements—especially when nesting if
statements:
<script>
const hour = new Date().getHours();
let greeting;
6
COM 2303, ICT 2204
switch statement
switch (expression) {
case label1:
statements1;
break;
case label2:
statements2;
break;
// …
default:
statementsDefault;
}
● The program first looks for a case clause with a label matching the value of
expression and then transfers control to that clause, executing the associated
statements.
● If no matching label is found, the program looks for the optional default clause:
● If a default clause is found, the program transfers control to that clause,
executing the associated statements.
● If no default clause is found, the program resumes execution at the statement
following the end of switch.
(By convention, the default clause is written as the last clause, but it does not need to be
so.)
break statements
The optional break statement associated with each case clause ensures that the
program breaks out of switch once the matched statement is executed, and then
continues execution at the statement following switch. If break is omitted, the program
7
COM 2303, ICT 2204
continues execution inside the switch statement (and will evaluate the next case, and so
on).
eg:
<script>
switch (fruitType) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
console.log("Mangoes are $0.56 a pound.");
break;
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
break;
default:
console.log(`Sorry, we are out of’,fruitType );
}
console.log("Is there anything else you'd like?");
</script>