Fundamentals 1 Flashcards

1
Q

What is a statement and an example of one?

A

Statements are syntax constructs and commands that perform actions. An example could be, alert(‘Hello, world!’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is it recommend putting semicolons between statements even if they are separated by newlines?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add one-line comments and multi-line comments?

A

One-line: // This is a single line comment

Multi-line: /* This is a multi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add one-line comments and multi-line comments?

A

One-line: // This is a single line comment

Multi-line: /* This is a multi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you add one-line comments and multi-line comments?

A

One-line: // This is a single line comment

Multi-line: /* This is a multi
line comment.
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How could you use hotkeys to comment out lines?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are nested comments supported?

A

No. It will produce an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does the browser console use “use strict”? If not, how can you enable it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a variable?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare a variable and a variable in which the value cannot be changed?

A
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!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Typically variables are named using camelCase. When would you simply use all caps?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Do Data Types matter in Javascript? Why?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are two “special numeric values”?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are mathematical operations safe in JavaScript? Why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is BigInt used? How to use it? Are they needed often?

A

BigInt numbers are rarely needed.

In JavaScript, the “number” type cannot represent integer values larger than (253-1). For most purposes that’s quite enough, but sometimes we need really big numbers, e.g. for cryptography.

A BigInt value is created by appending n to the end of an integer:
// the "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;
16
Q

How do you declare a string in JavaScript?

A
A string in JavaScript must be surrounded by quotes.
let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed another ${str}`;

Backticks `` are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}

Many developers will simply just opt to use backticks for all their strings. It is really just preference.

17
Q

Is there Char’s in JavaScript?

A

In some languages, there is a special “character” type for a single character. For example, in the C language and in Java it is called “char”.

In JavaScript, there is no such type. There’s only one type: string. A string may consist of zero characters (be empty), one character or many of them.

18
Q

What are booleans two values? What can boolean values also come as a result of?

A
The boolean type has only two values: true and false:
let nameFieldChecked = true; // yes, name field is checked
let ageFieldChecked = false; // no, age field is not checked
Boolean values also come as a result of comparisons:
let isGreater = 4 > 1;
alert( isGreater ); // true (the comparison result is "yes")
19
Q

What is the “null” value?

A

In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents “nothing”, “empty” or “value unknown”.

The code below states that age is unknown:
let age = null;
20
Q

What is the undefined value? Should you ever explicitly use it?

A

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned, then its value is undefined:
let age;
alert(age); // shows “undefined”

It isn’t recommend explicitly using it such as, age = undefined;. Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.

21
Q

What is the ‘object’ data type?

A

The object type is special.

All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.

The symbol type is used to create unique identifiers for objects - more on that later.

22
Q

What is the ‘typeof’ operator?

A

The typeof operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.

A call to typeof x returns a string with the type name:
typeof 0 // “number”

typeof 10n // “bigint”

typeof true // “boolean”

typeof Math // “object”

typeof alert // “function”

23
Q

What types are ‘alert’ and ‘prompt’? What do they do?

A

Functions.

alert: 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”.

prompt: 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 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 cancelled.

For instance:
let age = prompt('How old are you?', 100);
alert(`You are ${age} years old!`); // You are 100 years old!
24
Q

What type is ‘confirm’? What does it do?

A

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
25
Q

What are two limitations with the modal windows?

A
  1. The exact location of the modal window is determined by the browser. Usually, it’s in the center.
  2. The exact look of the window also depends on the browser. We can’t modify it.