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.