Fundamentals 1 Flashcards
What is a statement and an example of one?
Statements are syntax constructs and commands that perform actions. An example could be, alert(‘Hello, world!’);
Is it recommend putting semicolons between statements even if they are separated by newlines?
JavaScript interprets the line break as an “implicit” semicolon. This is called an automatic semicolon insertion. However, there are some cases where it fails to recognise this. So, it is recommended putting semicolons between statements even if they are separated by newlines.
How do you add one-line comments and multi-line comments?
One-line: // This is a single line comment
Multi-line: /* This is a multi
How do you add one-line comments and multi-line comments?
One-line: // This is a single line comment
Multi-line: /* This is a multi
How do you add one-line comments and multi-line comments?
One-line: // This is a single line comment
Multi-line: /* This is a multi
line comment.
*/
How could you use hotkeys to comment out lines?
In most editors, a line of code can be commented out by pressing the Ctrl+/ hotkey for a single-line comment and something like Ctrl+Shift+/ – for multiline comments (select a piece of code and press the hotkey). For Mac, try Cmd instead of Ctrl and Option instead of Shift.
Are nested comments supported?
No. It will produce an error.
Should you use, “use strict”; in your code?
Does it need to be placed on the first line?
Can you cancel out “use strict”; once you use it?
Yes, you should always use it, but you might not need to if all your code is in classes and modules.
It needs to be placed on the first line.
There is no way to cancel it, once you enter “strict mode”; there is no going back!
Does the browser console use “use strict”? If not, how can you enable it?
No it doesn’t.
Enable it like below, the comments are there for explanation.
‘use strict’; //press Shift+Enter for a new line
// …your code
// Press Enter to run
This will work in most browsers, namely Firefox and Chrome.
What is a variable?
A variable is a “named storage” for data. You can think of it like a box, in which you store things in.
An online shop – the information might include goods being sold and a shopping cart.
A chat application – the information might include users, messages, and much more.
Variables are used to store this information.
How do you declare a variable and a variable in which the value cannot be changed?
Standard Variables let message; // define variable let message = 'Hello!'; // define the variable and assign the value let user = 'John', age = 25, message = 'Hello'; // declare multiple variables in one line
Unchanging Values const myBirthday = '18.04.1982'; myBirthday = '01.01.2001'; // error, can't reassign the constant!
Typically variables are named using camelCase. When would you simply use all caps?
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
For instance, let’s make constants for colors in so-called “web” (hexadecimal) format: const COLOR_ORANGE = "#FF7F00"; // ...when we need to pick a color let color = COLOR_ORANGE; alert(color); // #FF7F00
Benefits:
COLOR_ORANGE is much easier to remember than “#FF7F00”.
It is much easier to mistype “#FF7F00” than COLOR_ORANGE.
When reading the code, COLOR_ORANGE is much more meaningful than #FF7F00.
Do Data Types matter in Javascript? Why?
Not really. Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them.
// no error let message = "hello"; message = 123456;
What are two “special numeric values”?
Special numeric values formally belong to the “number” type. Of course they are not numbers in the common sense of this word.
Infinity:
alert( 1 / 0 ); // Infinity
alert( Infinity ); // Infinity (referencing it directly)
NaN (Not a Number) represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance:
alert( “not a number” / 2 ); // NaN, such division is erroneous
NaN is sticky. Any further mathematical operation on NaN returns NaN:
alert( NaN + 1 ); // NaN
alert( 3 * NaN ); // NaN
alert( “not a number” / 2 - 1 ); // NaN
Are mathematical operations safe in JavaScript? Why?
Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
The script will never stop with a fatal error (“die”). At worst, we’ll get NaN as the result.