JS fundamentals Flashcards
What is alert function ?
It shows a message and waits for the user to press “OK”.
For example:
alert(“Hello”);
The mini-window with the message is called a modal window. The word “modal” means that the visitor can’t interact with the rest of the page, press other buttons, etc, until they have dealt with the window. In this case – until they press “OK”.
What is prompt function ?
The function prompt accepts two arguments:
result = prompt(title, [default]);
It shows a modal window with a text message, an input field for the visitor, and the buttons OK/Cancel.
title -The text to show the visitor.
default - An optional second parameter, the initial value for the input field.
The square brackets in syntax […] -The square brackets around default in the syntax above denote that the parameter is optional, not required.
The visitor can type something in the prompt input field and press OK. Then we get that text in the result. Or they can cancel the input by pressing Cancel or hitting the Esc key, then we get null as the result.
The call to prompt returns the text from the input field or null if the input was canceled.
What is confirm function ?
The syntax:
result = confirm(question);
The function confirm shows a modal window with a question and two buttons: OK and Cancel.
The result is true if OK is pressed and false otherwise.
For example:
let isBoss = confirm(“Are you the boss?”);
alert( isBoss ); // true if OK is pressed
How string conversion happens ?
String conversion happens when we need the string form of a value.
For example, alert(value) does it to show the value.
We can also call the String(value) function to convert a value to a string:
let value = true; alert(typeof value); // boolean
value = String(value); // now value is a string “true”
alert(typeof value); // string
String conversion is mostly obvious. A false becomes “false”, null becomes “null”, etc.
Numeric conversion
alert( “6” / “2” );
Output ?
3 , because it is automatically converted
How to explicitly convert to number ?
We can use the Number(value) function to explicitly convert a value to a number:
let str = "123"; alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number
Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered.
If the string is not a valid number, the result of such a conversion is NaN. For instance:
let age = Number(“an arbitrary string instead of a number”);
alert(age); // NaN, conversion failed
alert( Number(“ 123 “) );
123 without whitespaces
alert( Number(“123z”) );
NaN (error reading a number at “z”)
alert( Number(true) );
1
alert( Number(false) );
0
alert( Boolean(1) );
true
alert( Boolean(0) );
false
alert( Boolean(“hello”) );
true
alert( Boolean(“”) );
false
alert( Boolean(“0”) );
true
alert( Boolean(“ “) );
// spaces, also true (any non-empty string is true)
What is an operand ?
An operand – is what operators are applied to. For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2. Sometimes, people call these “arguments” instead of “operands”.
What is an operator ?
An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:
let x = 1;
x = -x;
alert( x ); // -1, unary negation was applied
An operator is binary if it has two operands. The same minus exists in binary form as well:
let x = 1, y = 3; alert( y - x ); // 2, binary minus subtracts values
alert( ‘1’ + 2 );
alert( 2 + ‘1’ );
// "12" // "21"
alert( +true );
alert( +”” );
// 1 0 It actually does the same thing as Number(...), but is shorter.
let counter = 1; let a = ++counter;
alert(a);
// 2
let counter = 1; let a = counter++;
alert(a);
// 1
let result = 5 > 4; alert( result );
let result = 5 > 4; // assign the result of the comparison alert( result ); // true
alert( ‘Z’ > ‘A’ );
alert( ‘Glow’ > ‘Glee’ );
alert( ‘Bee’ > ‘Be’ );
alert( ‘Z’ > ‘A’ ); // true
alert( ‘Glow’ > ‘Glee’ ); // true
alert( ‘Bee’ > ‘Be’ ); // true
To see whether a string is greater than another, JavaScript uses the so-called “dictionary” or “lexicographical” order.
In other words, strings are compared letter-by-letter.
“A” == “a” Are these equal ?
case matters. A capital letter “A” is not equal to the lowercase “a”. Which one is greater? The lowercase “a”. Why? Because the lowercase character has a greater index in the internal encoding table JavaScript uses (Unicode). We’ll get back to specific details and consequences of this in the chapter Strings.
alert( ‘2’ > 1 );
alert( ‘01’ == 1 );
alert( ‘2’ > 1 ); // true, string ‘2’ becomes a number 2
alert( ‘01’ == 1 ); // true, string ‘01’ becomes a number 1
When comparing values of different types, JavaScript converts the values to numbers.
let a = 0; alert( Boolean(a) );
let b = "0"; alert( Boolean(b) );
alert(a == b);
let a = 0; alert( Boolean(a) ); // false
let b = "0"; alert( Boolean(b) ); // true
alert(a == b); // true!
It is possible that at the same time:
1-Two values are equal.
2-One of them is true as a boolean and the other one is false as a boolean.
From JavaScript’s standpoint, this result is quite normal. An equality check converts values using the numeric conversion (hence “0” becomes 0), while the explicit Boolean conversion uses another set of rules.
alert( 0 == false );
alert( ‘’ == false );
alert( 0 == false ); // true
alert( ‘’ == false ); // true
A regular equality check == has a problem. It cannot differentiate 0 from false:
This happens because operands of different types are converted to numbers by the equality operator ==. An empty string, just like false, becomes a zero.
What to do if we’d like to differentiate 0 from false?
A strict equality operator === checks the equality without type conversion.
In other words, if a and b are of different types, then a === b immediately returns false without an attempt to convert them.
Let’s try it:
alert( 0 === false ); // false, because the types are different
There is also a “strict non-equality” operator !== analogous to !=.
The strict equality operator is a bit longer to write, but makes it obvious what’s going on and leaves less room for errors.
alert( null === undefined );
alert( null === undefined ); // false
These values are different, because each of them is a different type.
alert( null == undefined );
alert( null == undefined ); // true
There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of ==), but not any other value.
alert( null > 0 );
alert( null == 0 );
alert( null >= 0 );
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true