Module 3

unary arithmetic operators

let str = "123";
let n1 = +str;
let n2 = -str;
let n3 = -n2;
let n4 = +"abcd";

console.log(`${str} : ${typeof str}`); // -> 123 : string
console.log(`${n1} : ${typeof n1}`); // -> 123 : number
console.log(`${n2} : ${typeof n2}`); // -> -123 : number
console.log(`${n3} : ${typeof n3}`); // -> 123 : number
console.log(`${n4} : ${typeof n4}`); // -> NaN : number

Logical operators

The highest priority is negation !, then conjunction &&, and finally the alternative || .

const a = false;
const b = true;
const c = false;
const d = true;

console.log(a && b && c || d); // -> true
console.log(a && b && (c || d)); // -> false

Logical operators and non-Boolean values

logical NOT

First, the operand is temporarily converted to a Boolean value and only then it is treated with the appropriate operator action. Therefore, the NOT operator will always return either false or true.

let nr = 0;
let year = 1970;
let name = "Alice";
let empty = "";

console.log(!nr); // -> true
console.log(!year); // -> false
console.log(!name); // -> false
console.log(!empty); // -> true

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 first operand evaluates to false, and the second operand otherwise.

The OR operator will return its first operand if first operand evaluates to true, and the second operand otherwise.

console.log(true && 1991); // -> 1991
console.log(false && 1991); // -> false
console.log(2 && 5); // -> 5
console.log(0 && 5); // -> 0
console.log("Alice" && "Bob"); // -> Bob
console.log("" && "Bob"); // -> empty string


console.log(true || 1991); // -> true
console.log(false || 1991); // -> 1991
console.log(2 || 5); // -> 2
console.log(0 || 5); // -> 5
console.log("Alice" || "Bob"); // -> Alice
console.log("" || "Bob"); // -> Bob

Comparison operators

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.

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).

console.log(10 === 5); // -> false
console.log(10 === 10); // -> true
console.log(10 === 10n); // -> false
console.log(10 === "10"); // -> false
console.log("10" === "10"); // -> true
console.log("Alice" === "Bob"); // -> false
console.log(0 === false); // -> false
console.log(undefined === false); // -> false

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 .

console.log(10 == 5); // -> false
console.log(10 == 10); // -> true
console.log(10 == 10n); // -> true
console.log(10 == "10"); // -> true
console.log("10" == "10"); // -> true
console.log("Alice" == "Bob"); // -> false
console.log(0 == false); // -> true
console.log(undefined == false); // -> false
console.log(NaN == NaN); // -> false

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.

console.log(10 !== 5); // -> true
console.log(10 !== 10); // -> false
console.log(10 !== 10n); // -> true
console.log(10 !== "10"); // -> true
console.log("10" !== "10"); // -> false
console.log("Alice" !== "Bob"); // -> true
console.log(0 !== false); // -> true
console.log(undefined !== false); // -> true
console.log(10 != 5); // -> true
console.log(10 != 10); // -> false
console.log(10 != 10n); // -> false
console.log(10 != "10"); // -> false
console.log("10" != "10"); // -> false
console.log("Alice" != "Bob"); // -> true
console.log(0 !=  false); // -> false
console.log(undefined != false); // -> true
console.log(NaN != NaN); // -> true

User interaction

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.

Dialog boxes

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.

We have three dialog boxes available to use.

  • show an alert box,

    The alert method is a method of the object window, but for convenience, it can be used without the need to type window.alert, so both forms are correct and can be seen in real applications. The window object is a generalization of the browser window or tab, and gives the developer access to data related to the state of this window.

    The alert window will be visible until the user clicks the OK button visible on the popup.

  • confirm dialog box

    The difference between alert and confirm is that the confirm dialog box displays two buttons, the OK button and the Cancel button. Depending on the button pressed by the user, the confirm method returns a Boolean value. True is returned when the user closes the dialog box using the OK button, and false is returned when the user presses the Cancel button.

    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.

  • prompt dialog box

    Like the confirm dialog box, it contains the OK and Cancel buttons, but it also contains a single-line text field that allows the user to input text.

alert("Hello, World!")
window.alert("Hello, World! for the second time");

let remove = confirm("Remove all data?");
let message = remove ? "Deleting Data" : "Cancelled"
console.log(message);

let name = window.prompt("What is your name?", "John Doe");
name = name ? name : "anonymous";
let age = prompt("Hello " + name + " how old are you?");
alert(name + " is " + age + " years old");