JS Basics. Flashcards
What are comments?
1.) By default, each line of text in the source files of a program is considered a statement that should be executed. You can prevent certain lines from executing by putting a double slash before them: //. This turns the code into a comment.
(e.g)
// console.log(“Let’s do some math”);
console.log(4 + 7);
2.) During execution, the commented-out lines no longer produce results. As we hoped, they weren’t executed.
3.) You can also write comments by typing /* / around the code you want commented out.
(e.g)
/ A comment
written on
several lines */
4.) Adding comments to complicated or critical parts is a good habit you should build right now!
How do you display information while writing JS?
Both console.log() and alert() can be used to display information to the user. Unlike alert(), console.log() does not stop program execution and is often a better choice.
console.log() can also display several comma-separated values at once.
(e.g)
const temp1 = 36.9;
const temp2 = 37.6;
const temp3 = 37.1;
console.log(temp1, temp2, temp3); // Show “36.9 37.6 37.1”
What is the modern way of writing JS?
1.) The directive looks like a string: “use strict” or ‘use strict’. When it is located at the top of a script, the whole script works the “modern” way.
For example:
“use strict”;
// this code works the modern way
2.) Please make sure that “use strict” is at the top of your scripts, otherwise strict mode may not be enabled.
…
What is a Modal Window?
The mini-window which appears when a couple of functions to interact with the user: alert, prompt and confirm are run in the console.
How does JS interact with users on the browser?
1.) alert
shows a message.
2.) prompt
shows a message asking the user to input text. It returns the text or, if Cancel button or Esc is clicked, null.
3.) confirm
shows a message and waits for the user to press “OK” or “Cancel”. It returns true for OK and false for Cancel/Esc.
All these methods are modal: they pause script execution and don’t allow the visitor to interact with the rest of the page until the window has been dismissed.
What are || is Type Conversion?
1.) Most of the time, operators and functions automatically convert the values given to them to the right type.
2.) There are also cases when we need to explicitly convert a value to the expected type.
What are the main areas of string conversion?
1.) String Conversion.
String conversion is mostly obvious. A false becomes “false”, null becomes “null”, etc
let value = true;
alert(typeof value); // boolean
value = String(value); // now value is a string “true”
alert(typeof value); // string
2.) Numeric Conversions
3.) Boolean Conversion
How do Numeric Conversions work?
Numeric Conversion
Numeric conversion happens in mathematical functions and expressions automatically or We can use the Number(value) function to explicitly convert a value to a number:
Please note that null and undefined behave differently here: null becomes zero while undefined becomes NaN.
(e.g)
let str = “123”;
alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number
alert( Number(“ 123 “) ); // 123
alert( Number(“123z”) ); // NaN (error reading a number at “z”)
alert( Number(true) ); // 1
alert( Number(false) ); // 0
How do Boolean Conversions work?
1.) It happens in logical operations. but can also be performed explicitly with a call to Boolean(value).
2.)
Values that are intuitively “empty”, like 0,
an empty string,
null, undefined, and NaN,
become false.
Other values become true.
How do Comparisons work in JS?
1.) Comparison operators return a boolean value.
2.) Strings are compared letter-by-letter in the “dictionary” order.
3.) When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
4.) The values null and undefined equal == each other and do not equal any other value.
5.) Be careful when using comparisons like > or < with variables that can occasionally be null/undefined. Checking for null/undefined separately is a good idea.
What are Logical Operators?
There are four logical operators in JavaScript: || (OR), && (AND), ! (NOT), ?? (Nullish Coalescing).
What does OR || do?
1.) Evaluates operands from left to right.
For each operand, converts it to boolean.
2.) If the result is true, stops and returns the original value of that operand.
3.) If all operands have been evaluated (i.e. all were false), returns the last operand.
console.log(null || “user”)
// → user
console.log(“Agnes” || “user”)
// → Agnes
What is a Nullish Coalescing Operator ‘??’
1.) This is a recent addition to the language.
2.) Written as two question marks ??.
3.) The result of a ?? b is:
if a is defined, then a, if a isn’t defined, then b.
Whats the difference between ?? and || ?
1.) || returns the first truthy value.
2.) ?? returns the first defined value.
Who has Precedence?
1.) The precedence of the ?? operator is the same as ||. They both equal 3 in the MDN table.
2.) That means that, just like ||, the nullish coalescing operator ?? is evaluated before = and ?, but after most other operations, such as +, *.
3.) Due to safety reasons, JavaScript forbids using ?? together with && and || operators, unless the precedence is explicitly specified with parentheses.
4.) 1 + 1 == 2 && 10 * 10 > 50