JavaScript Essentials 1 (JSE) Module 3
JavaScript Essentials 1 (JSE) Module 3
Índice
JAVASCRIPT ESSENTIALS 1 (JSE) - MODULE 3 .............................................................................................................................4
JAVASCRIPT ESSENTIALS 1 (JSE) MODULE 3 ........................................................................................................................................................... 4
Operators and User Interaction ............................................................................................................................................................................. 4
3.1.1.1 JAVASCRIPT ESSENTIALS 1 (JSE) - MODULE 3 SECTION 1.......................................................................................5
JAVASCRIPT ESSENTIALS 1 (JSE): MODULE 3.......................................................................................................................................................... 5
SECTION 1................................................................................................................................................................................................................................ 5
Assignment, arithmetic, and logical operators .......................................................................................................................................... 5
3.1.1.2 OPERATORS .....................................................................................................................................................................................6
OPERATORS ............................................................................................................................................................................................................................ 6
3.1.1.3 ASSIGNMENT OPERATORS .......................................................................................................................................................7
ASSIGNMENT OPERATORS ................................................................................................................................................................................................. 7
3.1.1.4 ARITHMETIC OPERATORS .........................................................................................................................................................8
ARITHMETIC OPERATORS ................................................................................................................................................................................................... 8
3.1.1.5 UNARY ARITHMETIC OPERATORS ........................................................................................................................................9
UNARY ARITHMETIC OPERATORS..................................................................................................................................................................................... 9
3.1.1.6 UNARY INCREMENT AND DECREMENT OPERATORS ............................................................................................... 10
UNARY INCREMENT AND DECREMENT OPERATORS ................................................................................................................................................ 10
3.1.1.7 COMPOUND ASSIGNMENT OPERATORS ............................................................................................................................2
COMPOUND ASSIGNMENT OPERATORS ........................................................................................................................................................................ 2
3.1.1.8 LOGICAL OPERATORS ................................................................................................................................................................3
LOGICAL OPERATORS .......................................................................................................................................................................................................... 3
3.1.1.9 LOGICAL OPERATORS AND NON-BOOLEAN VALUES .................................................................................................5
LOGICAL OPERATORS AND NON-BOOLEAN VALUES .................................................................................................................................................. 5
3.1.1.10 LOGICAL OPERATORS AND NON-BOOLEAN VALUES- CONTINUED ..................................................................6
LOGICAL OPERATORS AND NON-BOOLEAN VALUES- CONTINUED ........................................................................................................................ 6
3.1.1.11 COMPOUND ASSIGNMENT OPERATORS..........................................................................................................................7
COMPOUND ASSIGNMENT OPERATORS ........................................................................................................................................................................ 7
3.1.1.12 OPERATORS - TASKS ................................................................................................................................................................8
TASKS ........................................................................................................................................................................................................................................ 8
Task 1 ........................................................................................................................................................................................................................................ 8
Task 2 ........................................................................................................................................................................................................................................ 8
Task 3 ........................................................................................................................................................................................................................................ 8
3.2.1.1 JAVASCRIPT ESSENTIALS 1 (JSE) - MODULE 3 SECTION 2.......................................................................................2
JAVASCRIPT ESSENTIALS 1 (JSE): MODULE 3.......................................................................................................................................................... 2
SECTION 2................................................................................................................................................................................................................................ 2
String, comparison, and other JS operators ................................................................................................................................................ 2
3.2.1.2 STRING OPERATORS ...................................................................................................................................................................3
STRING OPERATORS ............................................................................................................................................................................................................ 3
Compound Assignment Operators ..................................................................................................................................................................... 3
• know what operators are and how to classify them (by type of operand, by number of operands, etc.);
• be able to use assignment, arithmetic, logical, and comparison operators in practice;
• understand the operation of the conditional operator and the typeof, instanceof, and delete operators;
• understand what the precedence and associativity of basic operators are and be able to influence them
by means of bracket grouping;
• be able to perform basic two-way communication with the program user using the alert, confirm, and
prompt dialog boxes.
Section 1
Operators
Operators in programming languages are symbols (sometimes also names) that are used to perform certain
actions on arguments called operands.
Operands can be both values and variables. We have encountered operators several times in previous
examples, for example, the assignment symbol = or the keyword typeof .
Operators can be categorized in several ways. They are distinguished, for example, by the number of operands
they work on. The addition operator + is a typical binary operator (it uses two operands), while
the typeof operator is unary (it uses only one operand).
In JavaScript, there is also one ternary operator (operating on three operands), about which we will say a few
words in a moment.
We can differentiate between prefix operators (occurring before the operand), postfix operators (after the
operand) and infix operators (between operands). However, it’s common to categorize operators according to
the context in which they are used: so we have assignment; arithmetic; logical; or conditional operators. We
will further review the basic JavaScript operators according to this classification.
The same symbol can be interpreted as a different operator depending on the context, that is, most often, on the
type of operands. In JavaScript, the + symbol is one example. If operands are numbers, the use of this operator
will cause the interpreter to calculate their sum (it is an addition operator, classified as arithmetic). However, if
the operands are strings, the same symbol will be treated as a concatenation operator, and the interpreter will try
to join both strings of characters.
Assignment operators
Let's start with the assignment operators. In this group, there are operators that allow for the assigning of values
to variables and constants. The basic assignment operator is the equals sign =, which we have already seen
many times in the examples. This operator assigns the value of the right operand to the left operand.
If several assignment operators appear in a sequence, the order from right to left applies. So the sequence:
In addition to the basic assignment operator, there are also assignment operators connected to arithmetic,
logical, and string operators. We will come back to them when discussing the other operator categories.
Arithmetic operators
Arithmetic operators express mathematical operations, and they accept numerical values and variables. All
arithmetic operators, except addition, will try to implicitly convert values to the Number type before performing
the operation.
The addition operator will convert everything to a String if any of the operands is a String type, otherwise it will
convert them to a Number like the rest of the arithmetic operators. The order of the operations is respected in
JavaScript like in math, and we can use parentheses as in math to change the operation order if needed.
In general, it is a good habit to use parentheses to force the precedence and order of operations, not just
arithmetic. The precedence of operations performed by the interpreter will not always be as intuitive as the
precedence of arithmetic operations known from mathematics.
The basic binary arithmetic operators are the addition + , subtraction - , multiplication * , division / , division
remainder % and power ** . Their operation is analogous to what we know from mathematics, and the easiest
way to trace them is to use an example:
const x = 5;
const y = 2;
Both operators convert operands to the Number type, while the minus operator additionally negates it.
These operators in the postfix version (i.e. the operator is on the right side of the operand) performs the
operation by changing the value of the variable, but returns the value before the change. The prefix version of
the operator (i.e. the operator is placed before the operand) performs the operation and returns the new value.
Probably the easiest way to understand it is to use an example from the editor.
These are artefacts of floating-point arithmetic. The number will be precise for integers up to 252, but fractions
may not be as precise, as many fractions are impossible to directly represent in binary format. We’ll discuss how
to mitigate this in a moment when we introduce comparison operators.
Sandbox
app.js
let n1 = 10; let n3 = 20;
let n2 = 10; let n4 = 20;
Each of these operators takes a value from the variable to which the assignment is to be made (the left operand)
and modifies it by performing an arithmetic operation using the right operand value. The new value is assigned
to the left operand. For example, the code fragment below:
x += 100;
x = x + 100;
It should therefore not be difficult to understand how the following example works:
let x = 10;
x += 2;
console.log(x); // -> 12
x -= 4;
console.log(x); // -> 8
x *= 3;
console.log(x); // -> 24
x /= 6;
console.log(x); // -> 4
x **= 3;
console.log(x); // -> 64
x %= 10;
console.log(x); // -> 4
Logical operators
Logical operators work with Boolean type values ( true or false ). For now, we can assume that they work on
operands of this type and return values of this type only. JavaScript provides us with three such operators:
Their meaning is the same as in mathematical logic, and if you're not sure how they work, it's easiest to explain
them based on logical sentences.
Let's start with the conjunction. This is a binary operator that returns true if both operands are true. Using logical
sentences, we can imagine a sentence consisting of two simple statements connected by an AND, e.g.:
Both statements are true in this case, and after combining them with AND, a sentence is created which is also
true. If any of these statements were false (or both were false) the whole sentence would also be false, e.g:
In the case of an alternative that is also a binary operator, it is enough for one of the operands to be true for the
operator to return true. Coming back to our example with logical sentences, let's use a sentence made up of two
statements connected by an OR operator, e.g.:
The sentence may not look overly eloquent or sensible, but from the point of view of logic, it is quite correct. It is
enough that one of the statements is true, so that the whole sentence is also true. If both statements are false,
then the sentence will also be false, e.g.:
The negation operator is an unary, and it changes the logical value of the operand to its opposite, that is, false to
true, and true to false. Using logical sentences, we can present it with the negation NOT. Let's take as example
a simple sentence that is true:
London is a city.
In the same way, it will work the other way round, changing a false sentence into a true one. In the code, it will
look even simpler:
We can, of course, connect as many of these operators as we need, creating more complex “sentences”. As in
the case of arithmetic operators, the sequence of actions is determined here. The highest priority is negation ! ,
then conjunction && , and finally the alternative || . The precedence can of course be changed by means of
parentheses.
const a = false;
const b = true;
const c = false;
const d = true;
let nr = 0;
let year = 1970;
let name = "Alice";
let empty = "";
This is slightly different for binary logical operators (i.e. AND and OR). They don't return a Boolean value. In
reality, they return its first or second operand. The AND operator will return the first operand if it evaluates to
false, and the second operand otherwise. The OR operator will return its first operand if it evaluates to true, and
the second operand otherwise. Evaluation is simply an attempt to convert an operand to a Boolean-type value
(again, according to the rules learned in the previous chapter).
So, if the first operand of AND is false , it will be returned, and no other check will be performed.
Conversely, if the first operand of OR is true , it will be returned, and no other check will be made. This quickens
code execution, but has one side effect visible in this example:
let x = 0;
let y = 0;
console.log(x++ && y++); // -> 0
console.log(x); // -> 1
console.log(y); // -> y == 0
Logical operators are usually used together with conditional operators, and they are especially useful
in conditional instructions (decision making) and in loops (loop-ending conditions). You can learn about their
practical application in the sections on conditional instructions and loops mentioned just now.
It should be easy to imagine how they work. In the case of the AND operator, we can check it with the following
example:
let a = true;
console.log(a); // -> true
a &&= false;
console.log(a); // -> false
The instruction a &&= false means exactly the same as a = a && false .
let b = false;
console.log(b); // -> false
b ||= true;
console.log(b); // -> true
Tasks
Task 1
Arithmetic operators
Fill in the missing operators to get the expected result (replace the underscore symbol with the appropriate
operator):
Task 2
Comparison operators
Fill in the missing comparison operators in that such a way that all expressions result in true (replace the
underscore symbol with the appropriate operator):
Task 3
Logical operators
Fill in the missing comparison operators in that such a way that all expressions result in true (replace the
underscore symbol with the appropriate operator):
Section 2
String operators
The only operator in this group is the concatenation + . This operator will convert everything to a String if any of
the operands is a String type. Finally, it will create a new character string, attaching the right operand at the end
of the left operand.
Comparison operators
Comparison operators are used to check the equality or inequality of values. All comparison operators are
binary, and all of them return a logical value representing the result of the comparison, true or false .
As with other operators, JavaScript will try to convert the values that are being compared if they are of different
types. It makes sense to check equality, or which is greater, using numeric representation, and JavaScript will in
most cases convert types to a Number before comparison. There are two exceptions to this, strings and
the identity (strict equality) operator. Strings are compared char by char (precisely Unicode character by
Unicode character using their values).
To check if the operands are equal, we can use either the identity (strict equality) operator === or
the equality operator == .
The first is more restrictive, and in order to return true, the operands must be identical (i.e. they must be equal
and of the same type).
The equality operator requires that they are only equal, and their types are not compared. So if the operands are
of different types, the interpreter will try to convert them to numbers, for example, false will convert
to 0 , true to 1 , undefined to NaN , null to 0 , 10n to 10 and "123" to 123 , etc.
Note that if any of the operands has a NaN value (or has been converted to NaN , e.g. with undefined), the
equality operator will return false .
There are also complementary operators to those just demonstrated – the nonidentity operator !== and
the inequality operator != . The first returns true if the operands are not identical, in other words, they are equal
but of different types, or they are simply different. The second returns true if the operands are different.
You can also use them to compare strings that do not represent numbers, but the algorithm of this comparison is
quite complex, and the comparison itself is not very useful. By way of simplification, single characters of both
strings are tested on the same positions. It is assumed that the values of the single characters correspond to
their positions in the alphabet (the letter b has a higher value than the letter a). Upper-case letters have lower
values than lower-case letters, and digits have even lower values.
Note: the symbol => exists in JavaScript, but is not an operator – we use it in the construction of arrow functions.
Other operators
The list of operators in JavaScript is much longer, but many of them would not be particularly useful at this stage
of learning, such as bitwise operators, which operate on single bits of operands. However, it is worth mentioning
a few more operators, some of which have already appeared in earlier examples.
typeof
We already introduced the typeof operator when discussing data types. It is a unary operator, which checks the
type of operand (it can be a variable or a literal). The operator returns a string with the type name, such as
"boolean" or "number".
If you want to refresh your knowledge of this operator, go back to the section about data types.
instanceof
The instanceof operator appeared while discussing arrays. It is a binary operator that checks whether an object
(left operand) is of some type (right operand). Depending on the test result, it returns true or false.
During this course, the usefulness of this operator is limited to testing whether a variable contains an array.
delete
The unary delete operator was introduced while discussing objects. It allows you to delete a selected field of the
object whose name is indicated with an operand.
let user = {
name: "Alice",
age: 38
};
console.log(user.age); // -> 38
delete user.age;
console.log(user.age); // -> undefined
Each of these operands can be an expression that must be calculated. In the following example, the first
operand is a comparison of two numbers using a comparison operator. The result of the comparison will be
false, which will be used by the conditional (ternary) operator. Here we come to an important problem about
operator precedence and order of execution. In a moment, we will say a few more words about it.
Precedence
Practically in all the examples where we presented the operation of successive operators, we followed
instructions in which one operator was used. In reality, usually multiple operators are used simultaneously. At
this point, a quite important question arises: in what order will the interpreter perform them? This will of course
affect the final result of the operators, so it is worth taking this into account when writing the instructions.
let a = 10;
let b = a + 2 * 3;
let c = a + 2 < 20 - 15;
console.log(a); // -> 10
console.log(b); // -> 16
console.log(c); // -> false
In the second line of the example (variable b declaration), the operators are executed in the order we know from
mathematics. First, multiplication is performed, then addition, and at the end the resulting value is assigned to
the variable. In the third line (declaration of variable c) the matter gets a little more complicated. First, the sum of
variable a and number 2 is calculated, then the sum of numbers 20 and 15, and both results are compared with
the logical operator (less than) and the result is placed in variable c.
The JavaScript interpreter uses two operator properties to determine the sequence of operations: precedence
and associativity. Precedence can be treated as a priority, with some operators having the same precedence
(e.g. addition and subtraction). Associativity allows you to specify the order of execution if there are several
operators with the same priorities next to each other.
Precedence can be presented as a numerical value – the higher the value, the higher the priority of the
operation. If, for example, an OP1 operator has a smaller precedence than OP2, then the instruction:
a OP1 b OP2 c will be executed as follows: first, OP2 will be executed on operands b and c, then OP1 will be
executed on the left operand a and the right operand, resulting from OP2. So the instruction
could be presented in the form:
a OP1 ( b OP2 c) If we perform the same operations (or different operations but with the same precedence),
the interpreter uses associativity to determine the order of operations. Operators may have
a specified left-associativity (left to right order) or right-associativity (right to left order). Let's assume that in our
example, the operator OP1 has left-associativity:
a OP1 b OP2 c In such a situation, the OP1 operation on operands a and b will be performed first, followed
by a second OP1 operation on the received result and operand c. Bearing in mind that we
are dealing with left-associativity, we could write the instruction in the following form:
(a OP1 b) OP2 c It follows that it would be appropriate to know not only the precedence of all operators, but
also their associativity. This may seem a bit overwhelming, taking into account the number
of operators. Fortunately, it is usually enough to know the properties of the most basic operators and use
brackets in doubtful situations. The brackets allow you to impose the order of operations, just like in
mathematics. Keep this in mind when viewing the table below. It contains a list of operators we already know
with their precedence and associativity, so it is quite large. You absolutely do not have to remember everything if
you can use brackets to group operations.
Precedence - continued
Precedence Operator Associativity Symbol
14 Grouping n/a (…)
13 Field access ⇒ ….…
12 Function call ⇒ …(…)
Postfix increment n/a … ++
11
Postfix decrement n/a … --
Logical NOT ⇐ !…
Unary plus ⇐ +…
Unary negation ⇐ -…
11 Prefix increment ⇐ ++ …
Prefix decrement ⇐ -- …
typeof ⇐ typeof …
delete ⇐ delete …
9 Exponentiation ⇐ … ** …
Multiplication ⇒ …*…
8 Division ⇒ …/…
Remainder ⇒ …%…
Addition ⇒ …+…
7
Subtraction ⇒ …-…
Less than ⇒ …<…
Less than or equal ⇒ … <= …
6 Greater than ⇒ …>…
Greater than or equal ⇒ … >= …
instanceof ⇒ … instanceof …
Equality ⇒ … == …
Inequality ⇒ … != …
5
Strict Equality ⇒ … === …
Strict Inequality ⇒ … !== …
4 Logical AND ⇒ … && …
3 Logical OR ⇒ … || …
2 Conditional (ternary) ⇐ …?…:…
…=…
… += …
Assignment ⇐ … *= …
1
… and other assignment operators
An arrow in the associativity column facing the right side means left-to-right associativity, while facing the
opposite side means right-to-left associativity.
The abbreviation n/a means not applicable, because in some operators the term associativity does not make
sense.
At the very beginning of the table, there are three operators that may need further explanation:
• grouping is simply using brackets. They take precedence over the other operators, so we can use them
to force the execution of operations to take priority;
• field access (member access) is the operator used in dot notation, which is when accessing a selected
object field. It takes precedence over other operators (except for brackets), so for example the
instruction:
let x = myObject.test + 10; means that the value of the test field of the myObject object will be fetched
first, then we will add a value of 10 to it, and the result will go to the x variable;
• function call precedence tells us that if we call a function, this action will take priority over other
operations, except for grouping in brackets and the field access operator (dots). So in the example:
let y = 10 + myFunction() ** 2; myFunction will be called first, the result returned by it will be raised to
power 2, and only then we will add 10 to the total and save the result to variable y.
Remember, however, if you have any doubts, just use brackets to order the precedence of the operators used.
They allow you to organize even the most confusing instructions that come to mind.
let a, b;
b = (a = (20 + 20) * 2) > (3 ** 2);
console.log(a); // -> 80
console.log(b); // -> true
The use of parentheses not only forces the order of actions, but also increases the readability of the code (the
person reading it does not have to wonder what and in what order it will be done).
The full list of operators and properties can be found on the MDN pages.
Section Summary
In this chapter, we have introduced a few new operators (e.g. logical operators), and we have also solidified our
knowledge about a few that we already know (e.g. assignment operators). Together with the operators, new
terms describing their properties – precedence and associativity – have appeared.
It is likely that the amount of new information that has emerged, especially concerning operator precedence,
may seem a bit daunting. Don't worry, it is quite normal. In fact, there are probably not very many JavaScript
programmers who would be able to correctly set all operators according to their priorities.
Fortunately, we have parentheses to help, which makes it easier for us to force the priority of operations.
The operators will appear in all the examples until the end of the course, so you will gradually consolidate the
knowledge you've gained, which will surely become a logical and coherent whole over time.
Section 3
A simple example is the calculator program. The user not only enters data (e.g. the numbers 10 and 20), but
also decides what to do with it (e.g. sum up).
In the case of JavaScript, interaction with the user is quite a complex topic. This is for several reasons. Do you
remember that we can write programs in this language both for use in the browser (client-side) and to perform
certain actions on the server-side? This division influences the potential interaction. JavaScript programs written
with node.js for servers usually do not require such an interaction. We run them from the console level, often
without a graphic environment.
Most often, the user gives certain options (e.g. path data) to the program being run in this way in the form of a
configuration file and/or as call arguments. The data to run the program are then taken from disk files,
databases, network services, etc. It is very rare for such console programs to need to enter some data while the
program is running. If it is necessary, it is indicated by the appearance of appropriate information on the console,
on which you need to enter some data.
This is definitely different for client-side applications. In most cases, they require continuous interaction between
the user and the program. Using them, we can click on buttons, enter values into forms, select data from drop-
down lists, and so on. The problem is that practically all elements used for this purpose are HTML components.
Using them may not be very difficult, but it does require at least a thorough understanding of the basics of the
DOM (Document Object Model) used in web pages, and the basics of HTML itself
An example of an HTML page that uses two elements for interaction, for which JavaScript code is used, is
shown below:
<!DOCTYPE html>
<html>
<head></head>
<body>
<input id="myId" type="text"></input>
<button onclick="console.log(document.getElementById('myId').value)">Get
Text</button>
</body>
</html>
Run this code in the editor, making sure it is in the window dedicated to HTML (index.html tab).
The <input> element is an input field where you can enter any text. In our case, we’ve given this element
the myId identifier. The <button> element, as you can guess, corresponds to ... a button. Using
the onClick attribute, we have indicated that if the button is clicked, a piece of JavaScript code is to be run. In
this code, we refer to the document object (a fragment of the DOM model), which represents our website. We
search for the element with the myId identifier, retrieve its value (i.e. the text entered) and print the result on the
console.
There is another solution. Please note that it is not used in modern web applications, but it allows you to easily
give the user the opportunity to enter data or make certain decisions. We will use it in this course as a dummy
for normal communication, rather than as an element that you will find useful in real programming. The solution
is to use dialog boxes.
Dialog boxes
Dialog boxes are integral parts of web browsers, and available in almost all of them, even really old ones. All of
them are popup windows (or modal windows) which means that when the dialog box is displayed, it isn’t
possible to interact with the webpage itself until this dialog box is closed.
This inconvenience when the popup is visible is one of the reasons why we shouldn’t overuse them. They’re
perfectly fine for the learning process, and in some rare cases where important information needs to be
displayed, or some input from the user is mandatory, but they should be avoided in other circumstances. We
have three dialog boxes available to use.
alert("Hello, World!")
window.alert("Hello, World! for the second time");
alert(4 * 7);
alert(true);
Just like console.log, we can insert any value to the alert method and it will be converted to a string. The
difference is that we can put an arbitrary number of parameters to console.log , whereas with the alert we must
put only one (or zero, as it’s an optional parameter).
The alert window will be visible until the user clicks the OK button visible on the popup. Code execution will be
halted until the dialog box is closed.
The values true or false, returned by the confirm method as a result of the user's decision, allow for conditional
execution of some program actions. In the example below, we have additionally used a recently learned
conditional operator:
console.log(message);
As with other dialog boxes, the prompt accepts an optional parameter as a message that will be displayed.
The prompt also accepts a second optional parameter, which is the default value of the text field visible in the
dialog window. The same as confirm , the prompt method will return a result that is dependent on user input.
If the user closes the window with the OK button, anything in the text field will be returned from
the prompt method as a string. If the dialog box is closed with the Cancel button, the method will return a null
value. Due to the fact that on pressing the OK button, the value returned will be of the String type, we sometimes
need to convert it to another type, for example, to a Number type. As with all user input, we need to be prepared
for the fact that the data provided by the user can be invalid, either by mistake or on purpose, so always treat
values like this with extra caution.
let age = prompt("Hello " + name + " how old are you?");
alert(name + " is " + age + " years old");
Section Summary
Dialog windows may not be the most effective and elegant way to communicate with the program, but they will
be completely sufficient for our needs. They will allow you to retrieve data and take into account the user's
decisions, which will be able to influence the program."
Tasks
Task 1
Using everything you’ve learned up until this point, write a script that asks a user about the width, height, and
length of a box, then calculate and return to the user the volume of this box.
As an example, a box with width = 20 , height = 10 , and length = 50 will have a volume of 10000 (omitting units,
as they are not relevant in this scenario). For now, assume that the values provided by the user are valid
numbers, but if you have any ideas on how, you can try to make this script in such a way that it will be resistant
to invalid values.
Example
let width = prompt("Volume of the box, enter width", 0);
let height = prompt("Volume of the box, enter height", 0);
let length = prompt("Volume of the box, enter length", 0);
let volume = width * height * length;
alert(`Calculated box volume is ${volume}`);
LAB
Level of difficulty
Estimated time
Easy
15-30 minutes
Objectives
Familiarize the student with:
Scenario
Let's go back to our contact list. After some recent tweaks (i.e. using an array and objects) it is actually starting
to look like a list. However, one quite serious problem remained. Changing data in the list, such as adding a new
contact, has to be provided for right away in the program code. What if we want to give the user the ability to
enter the data of the added contact while the program is running? Modify the program to add, at the end of the
list, not the contact, which is given in the code, but the one which the user will give during the program run. Use
the prompt method to do this. At the end, display the first and the last contact from the list.
Sandbox
app.js
let contacts = [{
name: "Maxwell Wright",
phone: "(0191) 719 6495",
email: "Curabitur.egestas.nunc@nonummyac.co.uk"
}, {
name: "Raja Villarreal",
phone: "0866 398 2895",
email: "posuere.vulputate@sed.com"
}, {
name: "Helen Richards",
phone: "0800 1111",
email: "libero@convallis.edu"
}];
});
let last = contacts.length - 1;