Basics Flashcards
Is JavaScript case-sensitive?
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.
“Basics” (developer.mozilla.org). Retrieved July 3, 2023.
What are the rules for using semicolons in JavaScript?
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.
“Basics” (developer.mozilla.org). Retrieved July 3, 2023.
What are the three kinds of variable declarations in JavaScript?
The three kinds of variable declarations in JavaScript are:
-
var
: Declares a variable, optionally initializing it to a value. -
let
: Declares a block-scoped, local variable, optionally initializing it to a value. -
const
: Declares a block-scoped, read-only named constant.
“Declarations” (developer.mozilla.org). Retrieved July 3, 2023.
What are the rules for creating identifiers (variable names) in JavaScript?
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.
“Variables” (developer.mozilla.org). Retrieved July 3, 2023.
What is variable hoisting in JavaScript?
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.
“Variable hoisting” (developer.mozilla.org). Retrieved July 3, 2023.
What is the difference in scope for variables declared with var
, let
, and const
?
- 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. - Variables declared with
let
andconst
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.
“Declaring variables” (developer.mozilla.org). Retrieved July 3, 2023.
What are the two types of comments in JavaScript and how are they used?
There are two types of comments in JavaScript:
- Single-line comments: These start with two forward slash characters
//
. Everything from//
to the end of the line is a comment. - 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.
“Comments” (developer.mozilla.org). Retrieved July 3, 2023.
What is the difference between var
, let
and const
in JavaScript?
The main differences between var
, let
and const
in JavaScript are:
- Scoping rules:
var
is function-scoped, meaning a variable declared withvar
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. - Hoisting: Both
var
,let
andconst
declarations are hoisted to the top of their containing scope. However,var
will be initialized withundefined
, whereaslet
&const
will not be initialized. If you try to use alet
orconst
variable before declaration, you’ll get aReferenceError
. - Re-declaration: In the same scope,
var
andlet
allow you to re-declare the same variable, whileconst
does not.
“Declarations” (developer.mozilla.org). Retrieved July 3, 2023.
What are the different scopes a variable may belong to in JavaScript?
In JavaScript, a variable may belong to one of the following scopes:
- 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.
- Module scope: The scope for code running in module mode.
- 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.
-
Block scope: This is an additional scope to which variables declared with
let
orconst
can belong. This scope is created with a pair of curly braces (a block).
“Variable scope” (developer.mozilla.org). Retrieved July 3, 2023.
What are global variables in JavaScript and how can you access them?
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
.
“Global variables” (developer.mozilla.org). Retrieved July 3, 2023.
How do you declare a constant?
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.
“Constants” (developer.mozilla.org). Retrieved July 4, 2023.
Can you declare a constant with the same name as a function or variable in the same scope?
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 }
“Constants” (developer.mozilla.org). Retrieved July 4, 2023.
Does the const keyword prevent mutations?
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'];
“Constants” (developer.mozilla.org). Retrieved July 4, 2023.
How many data types are defined in the latest ECMAScript standard?
The latest ECMAScript standard defines eight data types.
“Data types” (developer.mozilla.org). Retrieved July 4, 2023.
What are the seven primitive data types in JavaScript?
- Boolean. true and false.
-
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.) -
undefined
. A top-level property whose value is not defined. - Number. An integer or floating point number. For example: 42 or 3.14159.
- BigInt. An integer with arbitrary precision. For example: 9007199254740992n.
- String. A sequence of characters that represent a text value. For example: “Howdy”.
- Symbol. A data type whose instances are unique and immutable.
“Data types” (developer.mozilla.org). Retrieved July 4, 2023.
What is the non-primitive data type in JavaScript?
Object
is the non-primitive data type in JavaScript.
“Data types” (developer.mozilla.org). Retrieved July 4, 2023.
What is the distinction between objects and functions in JavaScript?
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.
“Data types” (developer.mozilla.org). Retrieved July 4, 2023.
What does it mean that JavaScript is a dynamically typed language?
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.
“Data type conversion” (developer.mozilla.org). Retrieved July 5, 2023.
In JavaScript, is it possible to change the data type of a variable after its declaration? Provide an example.
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.
“Data type conversion” (developer.mozilla.org). Retrieved July 5, 2023.
How does JavaScript handle expressions involving numeric and string values with the +
operator?
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"
“Numbers and the ‘+’ operator” (developer.mozilla.org). Retrieved July 5, 2023.
How does JavaScript handle expressions involving numeric and string values with operators other than +
?
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
“Numbers and the ‘+’ operator” (developer.mozilla.org). Retrieved July 5, 2023.
What are the methods available in JavaScript for converting strings to numbers?
In JavaScript, you can convert strings to numbers using the parseInt()
, parseFloat()
, or unary plus +
operator methods.
“Converting strings to numbers” (developer.mozilla.org). Retrieved July 5, 2023.
What is the difference between parseInt()
and parseFloat()
in JavaScript?
-
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.
“Converting strings to numbers” (developer.mozilla.org). Retrieved July 5, 2023.
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?
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.
“Numbers and the ‘+’ operator” (developer.mozilla.org). Retrieved July 5, 2023.
How can you use the unary plus operator +
to convert strings to numbers in JavaScript?
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.
“Numbers and the ‘+’ operator” (developer.mozilla.org). Retrieved July 5, 2023.
What are literals in JavaScript?
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.
“Literals” (developer.mozilla.org). Retrieved July 6, 2023.
What is an array literal in JavaScript?
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.
“Array literals” (developer.mozilla.org). Retrieved July 6, 2023.
What happens when you put two commas in a row in an array literal in JavaScript?
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.
“Array literals” (developer.mozilla.org). Retrieved July 6, 2023.
What is the effect of a trailing comma at the end of an array literal in JavaScript?
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.
“Array literals” (developer.mozilla.org). Retrieved July 6, 2023.
What are the two literal values of the Boolean type in JavaScript?
The two literal values of the Boolean type in JavaScript are true
and false
.
“Boolean literals” (developer.mozilla.org). Retrieved July 6, 2023.
What are the different bases in which Integer and BigInt literals can be written in JavaScript?
Integer and BigInt literals can be written in JavaScript in decimal (base 10), hexadecimal (base 16), octal (base 8), and binary (base 2).
“Numeric literals” (developer.mozilla.org). Retrieved July 7, 2023.
What does a leading 0
or a leading 0o
indicate in JavaScript?
A leading 0
or a leading 0o
in JavaScript indicates that the integer literal is in octal (base 8).
“Numeric literals” (developer.mozilla.org). Retrieved July 7, 2023.
In JavaScript, what does a trailing n
suffix on an integer literal indicate?
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).
“Numeric literals” (developer.mozilla.org). Retrieved July 7, 2023.
Explain Integer literals in JavaScript
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 leading0o
(or0O
) indicates it is in octal. Octal integer literals can include only the digits0 – 7
. - A leading
0x
(or0X
) indicates a hexadecimal integer literal. Hexadecimal integers can include digits (0 – 9
) and the lettersa – f
andA – F
. (The case of a character does not change its value. Therefore:0xa = 0xA = 10 and 0xf = 0xF = 15
.) - A leading
0b
(or0B
) indicates a binary integer literal. Binary integer literals can only include the digits0 and 1
. - A trailing
n
suffix on an integer literal indicates aBigInt
literal. The integer literal can use any of the above bases. Note that leading-zero octal syntax like0123n
is not allowed, but0o123n
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)
“Numeric literals” (developer.mozilla.org). Retrieved July 7, 2023.
Explain Floating point literals in JavaScript
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
“Numeric literals” (developer.mozilla.org). Retrieved July 7, 2023.
What is E-notation in JavaScript and how is it used?
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.
“How to write Scientific notation literal in Javascript” (java2s.com). Retrieved July 7, 2023.
What is an object literal?
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.
“Object literals” (developer.mozilla.org). Retrieved July 10, 2023.
Why should an object literal not be used at the beginning of a statement in JavaScript?
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.
“Object literals” (developer.mozilla.org). Retrieved July 10, 2023.
In JavaScript, how can you access property names that are not valid identifiers?
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"]
.
“Object literals” (developer.mozilla.org). Retrieved July 10, 2023.
What is an enhanced object literal in JavaScript?
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, };
“Object literals” (developer.mozilla.org). Retrieved July 10, 2023.
In an enhanced object literal in JavaScript, how can you compute property names with expressions?
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
.
“Object literals” (developer.mozilla.org). Retrieved July 10, 2023.
How do you define a regexp literal
A regex literal is a pattern enclosed between slashes. The following is an example of a regex literal.
const re = /ab+c/;
“RegExp literals” (developer.mozilla.org). Retrieved July 10, 2023.