Basics Flashcards

1
Q

Is JavaScript case-sensitive?

A

Yes, JavaScript is case-sensitive and uses the Unicode character set. This means variable names such as 'Früh' and 'früh' are considered different because JavaScript is case sensitive.

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

What are the rules for using semicolons in JavaScript?

A

In JavaScript, instructions are called statements and are separated by semicolons (;). A semicolon is not necessary after a statement if it is written on its own line. However, if more than one statement on a line is desired, then they must be separated by semicolons.

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

What are the three kinds of variable declarations in JavaScript?

A

The three kinds of variable declarations in JavaScript are:

  1. var: Declares a variable, optionally initializing it to a value.
  2. let: Declares a block-scoped, local variable, optionally initializing it to a value.
  3. const: Declares a block-scoped, read-only named constant.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the rules for creating identifiers (variable names) in JavaScript?

A

A JavaScript identifier usually starts with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (0 – 9). Because JavaScript is case sensitive, letters include the characters A through Z (uppercase) as well as a through z (lowercase). You can use most of ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers.

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

What is variable hoisting in JavaScript?

A

Variable hoisting in JavaScript is the behavior where var-declared variables are treated as if they are declared at the top of their scope, regardless of where they are actually declared. This means you can refer to the variable anywhere in its scope, even if its declaration isn’t reached yet. However, if you access a variable before it’s declared, the value is always undefined, because only its declaration is hoisted, not its initialization.

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

What is the difference in scope for variables declared with var, let, and const?

A
  1. Variables declared with var are either function-scoped (if declared within a function) or globally scoped (if declared outside a function). They are not block-scoped.
  2. Variables declared with let and const are block-scoped, meaning they only exist within the block they were declared in. They can also be function-scoped or globally scoped if declared outside a block but inside a function or outside all functions, respectively.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the two types of comments in JavaScript and how are they used?

A

There are two types of comments in JavaScript:

  1. Single-line comments: These start with two forward slash characters //. Everything from // to the end of the line is a comment.
  2. Multi-line comments: These start with a forward slash and an asterisk /* and end with an asterisk and a forward slash */. Everything between /* and */ is a comment. Note that these types of comments cannot be nested unless the closing characters are escaped.

Comments are completely ignored by the JavaScript interpreter and have no effect on the executed code. They are used to make notes to yourself or to others who are reading your code.

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

What is the difference between var, let and const in JavaScript?

A

The main differences between var, let and const in JavaScript are:

  1. Scoping rules: var is function-scoped, meaning a variable declared with var is available throughout the whole function it is declared in. On the other hand, let & const are block-scoped, meaning they are available only within the block it’s declared in.
  2. Hoisting: Both var, let and const declarations are hoisted to the top of their containing scope. However, var will be initialized with undefined, whereas let & const will not be initialized. If you try to use a let or const variable before declaration, you’ll get a ReferenceError.
  3. Re-declaration: In the same scope, var and let allow you to re-declare the same variable, while const does not.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the different scopes a variable may belong to in JavaScript?

A

In JavaScript, a variable may belong to one of the following scopes:

  1. Global scope: The default scope for all code running in script mode. A variable declared outside of any function is considered global and is available to any other code in the current document.
  2. Module scope: The scope for code running in module mode.
  3. Function scope: The scope created with a function. When you declare a variable within a function, it is called a local variable and is only available within that function.
  4. Block scope: This is an additional scope to which variables declared with let or const can belong. This scope is created with a pair of curly braces (a block).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are global variables in JavaScript and how can you access them?

A

Global variables in JavaScript are properties of the global object. In web pages, the global object is the window, so global variables can be set and accessed using the window.variable syntax.

In all environments, the globalThis variable (which itself is a global variable) can be used to access global variables.

For example, if a variable called phoneNumber is declared in a document, you can refer to this variable from an iframe as parent.phoneNumber.

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

How do you declare a constant?

A
const PI = 3.14;

Note: Constants cannot change their value through assignment or be re-declared while the script is running. They must be initialized to a value.

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

Can you declare a constant with the same name as a function or variable in the same scope?

A

No, you cannot declare a constant with the same name as a function or variable in the same scope.

For example:

// THIS WILL CAUSE AN ERROR
function f() {}
const f = 5;

// THIS WILL CAUSE AN ERROR TOO
function f() {
  const g = 5;
  var g;
  //statements
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does the const keyword prevent mutations?

A

No, const only prevents re-assignments, but doesn’t prevent mutations. The properties of objects and the contents of arrays assigned to constants are not protected.

For example:

// Object mutation
const MY_OBJECT = { key: "value" };
MY_OBJECT.key = "otherValue";

// Array mutation
const MY_ARRAY = ["HTML", "CSS"];
MY_ARRAY.push("JAVASCRIPT");
console.log(MY_ARRAY); // ['HTML', 'CSS', 'JAVASCRIPT'];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many data types are defined in the latest ECMAScript standard?

A

The latest ECMAScript standard defines eight data types.

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

What are the seven primitive data types in JavaScript?

A
  1. Boolean. true and false.
  2. null. A special keyword denoting a null value. (Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.)
  3. undefined. A top-level property whose value is not defined.
  4. Number. An integer or floating point number. For example: 42 or 3.14159.
  5. BigInt. An integer with arbitrary precision. For example: 9007199254740992n.
  6. String. A sequence of characters that represent a text value. For example: “Howdy”.
  7. Symbol. A data type whose instances are unique and immutable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the non-primitive data type in JavaScript?

A

Object is the non-primitive data type in JavaScript.

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

What is the distinction between objects and functions in JavaScript?

A

While functions are technically a kind of object, you can think of objects as named containers for values, and functions as procedures that your script can perform.

18
Q

What does it mean that JavaScript is a dynamically typed language?

A

Being a dynamically typed language means you don’t have to specify the data type of a variable when you declare it. JavaScript also automatically converts data types as-needed during script execution.

19
Q

In JavaScript, is it possible to change the data type of a variable after its declaration? Provide an example.

A

Yes, it is possible. Because JavaScript is dynamically typed, you can change the data type of a variable after its declaration.

For example:

let answer = 42; // number
answer = "Thanks for all the fish!"; // string

This assignment does not cause an error message in JavaScript.

21
Q

How does JavaScript handle expressions involving numeric and string values with the + operator?

A

In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings.

For example:

x = "The answer is " + 42; // "The answer is 42"
y = 42 + " is the answer"; // "42 is the answer"
z = "37" + 7; // "377"
21
Q

How does JavaScript handle expressions involving numeric and string values with operators other than +?

A

With all other operators, JavaScript does not convert numeric values to strings, but rather it converts strings to numbers.

For example:

"37" - 7; // 30
"37" * 7; // 259
22
Q

What are the methods available in JavaScript for converting strings to numbers?

A

In JavaScript, you can convert strings to numbers using the parseInt(), parseFloat(), or unary plus + operator methods.

23
Q

What is the difference between parseInt() and parseFloat() in JavaScript?

A
  • parseInt() only returns whole numbers, which means it can’t be used for decimal numbers.
  • parseFloat(), on the other hand, can parse and return both integers and decimal numbers.
24
Q

What is the purpose of the radix parameter in parseInt() method in JavaScript and why is it considered a best practice to always include it?

A

The radix parameter in the parseInt() method is used to specify which numerical system is to be used. It’s considered a best practice to always include it to avoid confusion and ensure predictable behavior.

25
Q

How can you use the unary plus operator + to convert strings to numbers in JavaScript?

A

The unary plus + operator can be used to convert strings to numbers.

For example:

"1.1" + "1.1" // '1.11.1'
(+"1.1") + (+"1.1"); // 2.2
// Note: the parentheses are added for clarity, not required.
26
Q

What are literals in JavaScript?

A

Literals in JavaScript represent fixed values, not variables, that are directly provided in the script. These are static values which you explicitly write in your scripts.

27
Q

What is an array literal in JavaScript?

A

An array literal in JavaScript is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). It is used to create and initialize an array with specified values as its elements.

For example:

const coffees = ["French Roast", "Colombian", "Kona"]; 

Creates an array named coffees with three elements.

28
Q

What happens when you put two commas in a row in an array literal in JavaScript?

A

If you put two commas in a row in an array literal, the array leaves an empty slot for the unspecified element.

For example:

const fish = ["Lion", , "Angel"]; 

Creates an array with three elements, but the second element is empty.

29
Q

What is the effect of a trailing comma at the end of an array literal in JavaScript?

A

A trailing comma at the end of an array literal in JavaScript is ignored. It does not create an extra, undefined element. For instance:

const myList = ["home", , "school", ,]; 

Creates an array with four elements, and myList[1] and myList[3] are missing, there is not myList[4]. The last comma is ignored.

30
Q

What are the two literal values of the Boolean type in JavaScript?

A

The two literal values of the Boolean type in JavaScript are true and false.

31
Q

What are the different bases in which Integer and BigInt literals can be written in JavaScript?

A

Integer and BigInt literals can be written in JavaScript in decimal (base 10), hexadecimal (base 16), octal (base 8), and binary (base 2).

32
Q

What does a leading 0 or a leading 0o indicate in JavaScript?

A

A leading 0 or a leading 0o in JavaScript indicates that the integer literal is in octal (base 8).

33
Q

In JavaScript, what does a trailing n suffix on an integer literal indicate?

A

In JavaScript, a trailing n suffix on an integer literal indicates a BigInt literal. This literal can use any of the bases (decimal, hexadecimal, octal, binary).

34
Q

Explain Integer literals in JavaScript

A

Integer and BigInt literals can be written in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

  • A decimal integer literal is a sequence of digits without a leading 0 (zero).
  • A leading 0 (zero) on an integer literal, or a leading 0o (or 0O) indicates it is in octal. Octal integer literals can include only the digits 0 – 7.
  • A leading 0x (or 0X) indicates a hexadecimal integer literal. Hexadecimal integers can include digits (0 – 9) and the letters a – f and A – F. (The case of a character does not change its value. Therefore: 0xa = 0xA = 10 and 0xf = 0xF = 15.)
  • A leading 0b (or 0B) indicates a binary integer literal. Binary integer literals can only include the digits 0 and 1.
  • A trailing n suffix on an integer literal indicates a BigInt literal. The integer literal can use any of the above bases. Note that leading-zero octal syntax like 0123n is not allowed, but 0o123n is fine.

Some examples of integer literals are:

0, 117, 123456789123456789n             (decimal, base 10)
015, 0001, 0o777777777777n              (octal, base 8)
0x1123, 0x00111, 0x123456789ABCDEFn     (hexadecimal, "hex" or base 16)
0b11, 0b0011, 0b11101001010101010101n   (binary, base 2)
35
Q

Explain Floating point literals in JavaScript

A

A floating-point literal can have the following parts:

  • An unsigned decimal integer,
  • A decimal point (.),
  • A fraction (another decimal number),
  • An exponent.

The exponent part is an e or E followed by an integer, which can be signed (preceded by + or -). A floating-point literal must have at least one digit, and either a decimal point or e (or E).

More succinctly, the syntax is:

[digits].[digits][(E|e)[(+|-)]digits]

For example:

3.1415926
.123456789
3.1E+12
.1e-23
36
Q

What is E-notation in JavaScript and how is it used?

A

In JavaScript, E-notation is used to represent very large or very small numbers. It uses the format mEn, where m is the mantissa (the actual number part) and n is the exponent (the power of 10 to which m is raised).

For example, the number 2000 can be written as 2E3, where 2 is the mantissa and 3 is the exponent. This tells us that the number is 2 times 10 to the power of 3, or 2000.

Similarly, small numbers can be written using negative exponents. For example, 0.002 can be written as 2E-3, which means 2 times 10 to the power of -3, or 0.002.

E-notation is especially useful when dealing with very large or very small numbers, as it allows you to write these numbers in a more concise and manageable way.

37
Q

What is an object literal?

A

An object literal in JavaScript is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). The property names (also known as keys) and values can be any valid JavaScript expressions.

38
Q

Why should an object literal not be used at the beginning of a statement in JavaScript?

A

An object literal should not be used at the beginning of a statement because the opening curly brace { will be interpreted as the beginning of a block, which can lead to an error or unexpected behavior.

39
Q

In JavaScript, how can you access property names that are not valid identifiers?

A

Property names that are not valid identifiers can be accessed using bracket notation ([]). For example, if you have a property name that is an empty string or includes special characters (e.g., !), you can access it like this: object["property-name"].

40
Q

What is an enhanced object literal in JavaScript?

A

Enhanced object literals in JavaScript support additional shorthand syntaxes and features introduced in ES6. These include setting the prototype at construction, shorthand for foo: foo assignments (you can just write foo), defining methods directly, making super calls, and computing property names with expressions.

const obj = {
  \_\_proto\_\_: theProtoObj,
  // Shorthand for 'handler: handler'
  handler,
  // Methods
  toString() {
    // Super calls
    return "d " + super.toString();
  },
  // Computed (dynamic) property names
  ["prop_" + (() => 42)()]: 42,
};
41
Q

In an enhanced object literal in JavaScript, how can you compute property names with expressions?

A

In an enhanced object literal, you can compute property names by wrapping an expression in square brackets []. This expression is evaluated and the result is used as the property name. For example:

const obj = {
  ["prop_" + (() => 42)()]: 42,
};

In this example, the property name is computed to be "prop_42" and its value is 42.

42
Q

How do you define a regexp literal

A

A regex literal is a pattern enclosed between slashes. The following is an example of a regex literal.

const re = /ab+c/;