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.