JavaScript Foundation - Questions

Pack 1 192 questions

Things to know

  1. JavaScript Foundation unit required

Introduction

  1. Who created JavaScript?

    JavaScript was created by Brendan Eich.

  2. In which year was JavaScript first released?

    JavaScript was first released in 1995 with the Netscape Navigator 2.0 browser.

  3. What means when we say that JavaScript is a scripting language?

    Being a scripting language means that JavaScript is used to write 'scripts' that are run by another program, often termed as the engine of the language. Learn more in the section JavaScript Introduction — What is JavaScript exactly?

  4. What means when we say that JavaScript is a high-level language?

    Being a high-level language means that a lot of computing complexity is abstracted away from a developer coding in JavaScript. Contrast this to a low-level language such as assembly where you have to manually deal with a lot of programming concerns, such as memory, encapsulation, and so on.

    Learn more in the section JavaScript Introduction — What is JavaScript exactly?

  5. What is meant by an implementation of JavaScript?

    An implementation of JavaScript refers to a program that is capable of parsing JavaScript and then executing it. Learn more in JavaScript Introduction — What is an implementation?

  6. Name one implementation of JavaScript.

    V8, developed by Google, is an implementation of JavaScript. SpiderMonkey is another implementation, developed by Mozilla. There are many implemenations.

  7. What programming paradigms does JavaScript support?

    JavaScript supports the procedural, object-oriented, imperative, event-driven, and functional programming paradigms.

  8. Where do we write JavaScript code so that it could be run in the browser?

    JavaScript code can either go directly inside the <script> tag, which can then be placed on an HTML document inside <head> or <body>, or it can go inside a separate .js file that's consequently linked to an HTML page using the same <script> tag.

    Learn more in JavaScript Get Started.

  9. What is an external JavaScript file?

    An external JavaScript file is a file with a .js extension, where we can write JavaScript code, linked to an HTML page via a <script> tag. Learn more in JavaScript Get Started — External JavaScript files.

  10. How is a JavaScript file linked to in a webpage?

    Using the <script> tag and then its src attribute to specify the path of the file. Learn more in JavaScript Get Started — External JavaScript files.

  11. List 3 benefits of using an external JavaScript file for a webpage.

    1. A file could be cached to improve performance
    2. A file improves the maintainability of code, and the overall project.
    3. A file helps us produce portable code that could be used in third-party apps as well.

    Learn more in JavaScript Get Started — Benefits of using JavaScript files.

Console

  1. What is the console?

    The console is a tool made to provide JavaScript developers with a REPL environment for executing JavaScript on-the-go. Besides this is also helps developers to test their applications, by making logs and then inspecting the output of those logs in the console. Not only this, but is also allows developers to get to know of network errors in their web apps.

    Learn more in JavaScript Console.

  2. What is meant when we say that the console is a REPL environment for executing JavaScript?

    REPL stands for Read-Evaluate-Print-Loop and represents a program that reads the user's input, then evaluates it (in realtime), and then prints the result; all of this happening in a loop waiting for the user's input. The console is an example of REPL program made to test JavaScript quickly.

    Learn more in JavaScript Console.

  3. The sole function of the console is to execute JavaScript. True or false? If false, then what else can it do?

    False. As stated above, besides being able to execute JavaScript, the console can be used to test and debug JavaScript programs, and even inspect network errors occurring in a web app. Learn more in JavaScript Console.

  4. How to log anything to the console?

    Using console.log(). Learn more in JavaScript Console — Logging using console.log().

Basics

  1. What are integers?

    Integers are whole numbers, i.e. numbers without a fractional part. Examples are 1, 2, 3, 1000, -5, -50, etc.

  2. What are floats?

    Floats, or floating-point numbers, are numbers with a fractional part. Examples include 1.01, 2.45, 3.00000001, 1000.5, -5.111, -50.2478, etc.

  3. JavaScript has separate types for integers and floats. True or false? If false, then give the correct explanation.

    False. JavaScript has only one type for numbers.

  4. Name any five basic arithmetic operators that JavaScript provides along with their symbols.

    Here are all the the six arithmetic operators of JavaScript: addition (+), subtraction (-), multiplication (*), division (/), exponentiation (**) and modulo (%).

  5. What is meant when we say that the multiplication operator has a higher precedence than the addition operator in JavaScript?

    It means when both the multiplication and the addition operators exist in an operation, without any parentheses (()), multiplication would be performed first. For example, in the expression 2 * 5 + 10, multiplication would happen first and then addition, giving the result 20 (instead of 30 if addition was done first).

    Learn more in JavaScript Basics — Basic arithmetic.

  6. How can we enforce a particular operation to happen before the other?

    By using a pair of parentheses (()). Learn more in JavaScript Basics — Basic arithmetic.

  7. What is a string?

    A string is a sequence of textual characters used to represent literal text in a program. Learn more in JavaScript Basics — Strings.

  8. Give the three ways to denote a string in JavaScript.

    Using a pair of single quotes (''), a pair of double quotes (""), or a pair of backticks (``). Learn more in JavaScript Basics — Strings.

  9. What's the difference between single-quoted and double-quoted strings in JavaScript?

    Functionally, there's absolutely no difference between single-quoted and double-quoted strings in JavaScript. It's just that single-quoted strings can contain double quotes (") in them without the need for escaping, and vice versa. Learn more in JavaScript Basics — Strings.

  10. Can a single- or double-quoted string in JavaScript span multiple lines in source code?

    Neither a single-quoted, nor a double-quoted string in JavaScript could span multiple lines in source code. It's invalid to do so. Learn more in JavaScript Basics — Strings.

  11. What is an escape sequence?

    An esacpe sequence denotes a special character in a JavaScript string. It begins with the backslash (\) symbol and is followed by a character to, as a whole, represent the special character. An example is \n used to denote a new line. Learn more in JavaScript Basics — What's an escape sequence?

  12. List any three escape sequences.

    Here are some escape sequences: \n, \t, \r, \\, \', \". Learn more in JavaScript Basics — Strings — Escape sequences.

  13. How to denote new lines in single-quoted or double-quoted strings?

    Using the newline character \n. For instance, 'For\nyou.' and "For\nyou.".

  14. What is string concatenation?

    The action of joining together two strings into one single string is commonly referred to as string concatenation. Learn more in JavaScript Basics — Strings — Concatenation.

  15. Which symbol is used to perform string concatenation in JavaScript?

    The + symbol, which is also used to perform arithmetic addition. Learn more in JavaScript Basics — Strings — Concatenation.

  16. What is a variable?

    There are multiple ways of defining a variable. For example:

    • A variable is a container for holding data.
    • A variable is a name in a program, referring to a location in memory where some sort of data is stored.

    Learn more in JavaScript Basics — What are variables?

  17. How to create a variable in JavaScript?

    A variable in JavaScript can be created using var and let.

  18. Define the term 'keyword' in JavaScript. Give an example of a keyword.

    A keyword in JavaScript is a word reserved for special use in the language. Keywords can't be used as variable names. Some examples are var, let, const, etc.

  19. What is variable declaration?

    A variable declaration introduces a new variable into the current execution context. var x (or let x) is referred to the declaration of x. Learn more in JavaScript Basics — Variables.

  20. What is variable initialization?

    Variable initialization is when a variable is assigned — or better to say, initialized with — a value right at the time of its declaration. Learn more in JavaScript Basics — Variables.

  21. What is variable assignment?

    Variable assignment referes to a statement that serves to assign a given value to a variable. By that means, variable initialization is also an instance of variable assignment. Learn more in JavaScript Basics — Variables.

  22. How is variable initialization different from variable assignment?

    Variable assignment refers to a statement that serves to assign a given value to a variable. By that means, variable initialization is also an instance of variable assignment. Learn more in JavaScript Basics — Variables.

  23. In JavaScript, a variable can hold values of any arbitary type. True or false? If false, then give the correct explanation.

    Absolutely true. JavaScript is a dynamically-typed language and so a variable can hold any value, be it of any data type whatsoever. There's no restriction to stay bound to a given type. Learn more in JavaScript Basics — Variables.

  24. Name the three different kinds of dialog boxes that could be shown using JavaScript.

    Alert dialogs, prompt dialogs and confirmation dialogs. Learn more in JavaScript Basics — Dialog Boxes.

  25. Give the corresponding functions that can be used to show these dialogs.

    alert() is used to show alert dialogs, prompt() is used to show prompt dialogs, and confirm() is used to show confirmation dialogs. Learn more in JavaScript Basics — Dialog Boxes.

  26. What kind of values does confirm() return?

    confirm() returns back a Boolean value.

  27. What does the second argument to prompt() do?

    It specifies the initial value of the input field in the dialog box. By default, it's value is the empty string '', i.e. the input field is empty to begin with.

  28. prompt() always returns a string, even if the Cancel button is clicked. True or false? If false, then give the correct explanation.

    False. When the Cancel button is clicked, prompt() returns null. Learn more in JavaScript Basics — Dialog Boxes — prompt().

  29. How to convert a string to a number?

    Using the Number() function. Learn more in JavaScript Basics — Conversion to a number.

Variables

  1. What are the different variations of declaring variables using var?

    var could be used to only declare a single variable:

    var x;

    Or multiple variables could be declared in one go:

    var x, y, z;

    The variables could also be initialized to given values:

    var x = 10;
    var y = 2, z = 3;

    Learn more in JavaScript Variables — Variations of using var.

  2. Name three rules of naming variables in JavaScript.

    A variable's name can't begin with a digit; it can't have symbols in it, except for $ and _; it can't have spaces in between; and it can't be a reserved keyword. Learn more in JavaScript Variables — Rules for naming variables.

  3. Which casing convention is commonly used to name things in JavaScript?

    The camel casing convention. Learn more in JavaScript Variables — Casing conventions.

  4. How to check whether a given variable exists?

    Using the typeof operator. Learn more in JavaScript Variables — The typeof operator.

  5. What is variable hoisting?

    The act of processing all var declarations before executing any code is referred to as variable hoisting. Learn more in JavaScript Variables — Hoisting.

  6. What is the let keyword?

    The let keyword is yet another way to define variables in JavaScript, apart from using var. In particular, it's used to define block-scoped variables. Learn more in JavaScript Variables — The let keyword.

  7. Give two differences between let and var.

    let is used to create block-scoped variables while var isn't. Moreover, let declarations aren't hoisted while var are. Learn more in JavaScript Variables — The let keyword.

  8. What is meant by the temporal dead zone of a variable creating using let?

    The temporal dead zone of a let-declared variable refers to the region before the variable's declaration in the source code (in the current scope) where it's invalid to access it. Learn more in JavaScript Variables — The let keyword.

Constants

  1. What are constants in programming?

    Constants are named identifiers in a program that, once assigned a value, can't be changed. Learn more in JavaScript Constants.

  2. How to define a constant in JavaScript?

    Using the const keyword. Learn more in JavaScript Constants.

  3. A constant can be defined without an initializer. True or false? If false, then give the correct description.

    False. It's a must to define a constant with an initializer. Learn more in JavaScript Constants.

  4. What is screaming snake casing and why is it commonly used to name constants in programming?

    Screaming snake casing is a casing convention commonly used to name constants in JavaScript and other programming languages. In this casing, all words are uppercased and separated from others using an underscore (_). An example is MAX_SAFE_INTEGER.

    Learn more in JavaScript Constants.

  5. What are the rules for naming constants in JavaScript?

    The rules for naming constants are the exact same as those for naming variables in JavaScript. Learn more in JavaScript Constants — Naming constants.

Comments

  1. What are comments in programming?

    Comments are statements in a program that are ignored, i.e. they just don't get executed. Learn more in JavaScript Comments.

  2. Give two uses of comments in programming.

    Comments are used to explain code; to document code; and to test given code. Learn more in JavaScript Comments.

  3. Name the two kinds of comments in JavaScript.

    Single-line comments and multi-line comments. Learn more in JavaScript Comments — How to comment?

  4. How to write a comment of each kind?

    Single-line comments are denoted using // whereas multi-line comments are denoted using /* */. Learn more in JavaScript Comments — How to comment?

  5. Give two tips for writing good comments.

Data types

  1. What is a data type?

    A data type is a set of values along with the operations capable to be performed on those values. Learn more in JavaScript Data Types.

  2. What are primitives and objects in JavaScript?

    A primitive is the simplest form of data. An object is data made using primitives. Learn more in JavaScript Data Types — Primitives and objects.

  3. Give one difference between primitives and objects?

    Primitives have no properties/methods available on them whereas objects do have. Learn more in JavaScript Data Types — Primitives and objects.

  4. Name the seven primitive types of JavaScript.

    undefined, null, numbers, strings, Booleans, symbols, and big integers.

  5. Take the numbers 10 and 10.5. JavaScript considers these numbers to be of distinct types. True or false? If false, then give the correct explanation.

    False. Both 10 and 10.5 are considered by JavaScript to be of the same type, i.e. mere numbers. Learn more in JavaScript Data Types — Numbers.

  6. What is a number literal?

    A number literal is the exact — the 'literal' — representation of a number in source code. An example is 10. Learn more in JavaScript Data Types — Numbers.

  7. What is meant by the index of a character of a string?

    The index of a character refers to its position in the string. Learn more in JavaScript Data Types — Strings.

  8. What is the total number of characters in a string called?

    The length of the string. Learn more in JavaScript Data Types — Strings.

  9. How to retrieve the total number of characters of a string in JavaScript?

    By accessing its length property. Learn more in JavaScript Data Types — Strings.

  10. How can a primitive in JavaScript have a property available on it? Explain the phenomenon that enables this?

    Primitives in JavaSript can never have properties on them. But, from our perspective, we are able to access properties on primitives. The phenomenon that enables this is called autoboxing whereby the JavaScript engine automatically wraps a primitive value into an object and then accesses the property on that object.

    Learn more in JavaScript Data Types — What is autoboxing?

  11. What is a Boolean?

    A Boolean is a true/false value in programming. Learn more in JavaScript Data Types — Booleans.

  12. Give the two Boolean values of JavaScript.

    true and false. Learn more in JavaScript Data Types — Booleans.

  13. What is the purpose of the special value undefined in JavaScript?

    undefined is used to represent the absence of a value. Learn more in JavaScript Data Types — undefined.

  14. What is the purpose of the special value null in JavaScript?

    null is used to represent a value that is empty itself. It's commonly used in places where an object is expected otherwise. Learn more in JavaScript Data Types — null.

  15. What is an array?

    An array is a ordered sequence of elements. Learn more in JavaScript Data Types — Arrays.

  16. What is meant by an 'element' of an array?

    An element of an array simply refers to one of the items stored in it. Learn more in JavaScript Data Types — Arrays.

  17. What is meant by the index of an element of an array?

    It's simply the position of the element in the array. Learn more in JavaScript Data Types — Arrays.

  18. How to access the fifth element of an array stored in the variable arr?

    Via arr[4] (remember that indexes begin at 0).

  19. What is the total number of items in an array called?

    The length of the array.

  20. How to retrieve the total number of items of an array?

    Using its length property. Learn more in JavaScript Data Types — Arrays.

  21. In JavaScript, the items of an array all have to be of the same type. True or false? If false, then give the correct explanation.

    False. The items of an array can be of differing data types. Learn more in JavaScript Data Types — Arrays.

  22. What is an array literal?

    An array literal is the direct — the 'literal' — representation of an array in source code. It's denoted using a pair of square brackets ([]). Learn more in JavaScript Data Types — Arrays.

  23. What happens when we access an out-of-range index of an array in JavaScript? Do we get an error?

    We get undefined returned. No error occurs. Learn more in JavaScript Data Types — Arrays.

  24. Which method can be used to sort an array?

    The sort() method. Learn more in JavaScript Data Types — Arrays.

  25. What means when we say that a method modifies an array in-place?

    Modification in-place means that the original array is modified, not a copy of it. Learn more in JavaScript Data Types — Arrays.

  26. What is a function?

    A function defines a piece of code that gets executed when the function is called. Learn more in JavaScript Data Types — Functions.

  27. Functions in JavaScript can be named or anonymous. True or false? If false, then give the correct description.

    Absolutely true. Learn more in JavaScript Data Types — Functions.

  28. Which keyword is used to define functions in JavaScript?

    The function keyword. Learn more in JavaScript Data Types — Functions.

  29. What is the difference between function parameters and arguments?

    An argument refers to the value provided to a function when calling it whereas a parameter refers to the name, i.e. the variable, used inside the function's definition to access that value. Learn more in JavaScript Data Types — Functions.

  30. What is meant by the body of a function?

    The body of a function is the block of code that gets executed when the function is called. Another way to think about it is that it refers to the code inside the curly braces ({}) when defining a function. Learn more in JavaScript Data Types — Functions.

  31. What is meant by the definition of a function?

    The definition of a function refers to the entire block of code defining a function. Learn more in JavaScript Data Types — Functions.

  32. What is meant by invoking a function?

    Invoking a function, also known as calling the function, means to execute the function's body. Learn more in JavaScript Data Types — Functions.

  33. How to invoke a function stored in a variable func in JavaScript?

    Via func().

  34. How to return a given value from a function?

    Using the return keyword.

  35. What is an object?

    An object is a set of properties and methods. Learn more in JavaScript Data Types — Objects.

  36. What is an object literal?

    An object literal is the direct — the 'literal' — representation of an object in source code. It's denoted using a pair of curly braces ({}). Learn more in JavaScript Data Types — Objects.

  37. What is meant by the property of an object?

    An object's property defines one of its characteristics. Learn more in JavaScript Data Types — Objects.

  38. How to access the property named bar of an object stored in the variable foo?

    Either via foo.bar or foo['bar'] (or even foo["bar"]). Learn more in JavaScript Data Types — Objects.

  39. What is the purpose of the typeof keyword in JavaScript?

    typeof is used to determine the type of a given value. Learn more in JavaScript Data Types — typeof.

  40. The typeof keyword represents an expression. True or false? If false, then give the correct description.

    True. The typeof keyword represents an expression and, likewise, could be used anywhere where an expression is expected. Learn more in JavaScript Data Types — typeof.

  41. What does typeof null return?

    'object'. This was a bug in JavaScript that has been kept to date for backwards-compatibility. Learn more in JavaScript Data Types — typeof.

  42. What does typeof return when used to inspect a function?

    'function'. Learn more in JavaScript Data Types — typeof.

  43. JavaScript is a dynamically-typed language. What does this mean?

    The types of values stored in variables can change during the course of a program. Learn more in JavaScript Data Types — Dynamically-typed nature of JavaScript.

  44. Give an example of a statically-typed language.

    Java, C, C++, TypeScript, and so on.

Control Flow

  1. What is meant by control flow?

    Control flow basically refers to the flow of 'control', i.e. the flow of execution, in a computer program.

  2. What are conditional statements?

    Conditional statements are statements used to execute code based on the outcome of a given condition.

  3. What are iteration statements?

    Iteration statements are statements used to repeatedly execute code based on the outcome of a given condition.

  4. What does the if statement do?

    The if statement is used to execute a piece of code if a given condition is met. Learn more in JavaScript Control Flow — if.

  5. The body of an if statement can be a block statement. True or false? If false, then give the correct description.

    True. In fact, this is the case most of the times because typically we want to execute more than one statement inside an if's body and that could only be done via a block statement. Learn more in JavaScript Control Flow — if.

  6. What is a relational operation?

    A relational operation inspects the relationship between two values and then returns a Boolean value to imply that relationship. Learn more in JavaScript Control Flow — What is a relational operation?

  7. What does the else statement do?

    The else statement is used to execute code when the preceding if statement's condition isn't met. Learn more in JavaScript Control Flow — else.

  8. Name any five relational operators in JavaScript along with their symbols.

    Here are five relational operators in JavaScript amongst many others: less-than (<), less-than-equal-to (<=), greater-than (>), greater-than-equal-to (>=), identity (===). Learn more in JavaScript Control Flow — else.

  9. What does the switch statement do?

    It's used to execute code based on whether a given expression matches a given value. Learn more in JavaScript Control Flow — switch.

  10. What does the case keyword do?

    The case keyword is used to define a case for a switch statement. Learn more in JavaScript Control Flow — switch.

  11. What does the default keyword do?

    The default keyword is used to define the default case for a switch statement, which gets executed when neither of the preceding cases yields a match. Learn more in JavaScript Control Flow — switch.

  12. The default keyword can be used outside switch. True or false?

    False. The default keyword can only be used inside switch. Learn more in JavaScript Control Flow — switch.

  13. What is the for statement used for?

    The for statement is used to repeatedly execute a piece of code a given number of times. Learn more in JavaScript Control Flow — for.

  14. Describe the typical syntax of a for statement.

    We start with the for keyword, followed by a pair of parentheses (()), followed by the loop's body. Inside the parentheses, we have three discrete parts: the first one is the initialization statement, the second one is the condition on which the loop runs, and the third one is an expression modifying the variables involved in the loop's condition.

    Learn more in JavaScript Control Flow — for.

  15. What is meant by a counter variable?

    A variable used to keep track of the number of iterations of a loop is referred to as a counter variable. Learn more in JavaScript Control Flow — for.

  16. What is the postfix increment operator?

    The postfix increment operator, denoted as ++, comes after a variable and is used to increment the value of the variable by 1. For example, num++ is the same as saying num = num + 1. Learn more in JavaScript Control Flow — for — The postfix increment operator (++).

  17. Why is the counter variable of a for loop typically initialized to 0?

    So that it's easy to use the loop to iterate over an array, as array indexes begin at 0. Learn more in JavaScript Control Flow — for — Why is the counter typically initialized to 0 in for?

  18. What is the while statement used for?

    The while statement is used to repeatedly execute a piece of code as long as a given condition is met. Learn more in JavaScript Control Flow — while.

  19. What's the difference between for and while?

    Functionally, there is no difference. It's just that for is more suitable for iterating a known number of times, whereas while is more suitable for iterating an unknown number of times as long as a given condition is met. It's important to remember that whatever could be achieved using for, could be achieved using while as well, and vice versa.

    Learn more in JavaScript Control Flow — while.

Functions

  1. What is a function?

    A function, in JavaScript, represents a block of code that can be executed at any time when the function is called. Learn more in JavaScript Functions.

  2. How to create a function in JavaScript using the function keyword?

    First comes the function keyword, followed by the name of the function, followed by a pair of parentheses (()), followed by a pair of curly braces ({}).

    function functionName() {
       statements;
    }

    Learn more in JavaScript Functions — Creating a function.

  3. What is a function's definition?

    A function's definition refers to the set of code defining it. Learn more in JavaScript Functions — Creating a function.

  4. What is a function's body?

    A function's body refers to the block of code following the function's parameters in the function's definition. Learn more in JavaScript Functions — Creating a function.

  5. What does it mean to invoke a function?

    To invoke a function, or call a function, means to execute its body. Learn more in JavaScript Functions — Creating a function.

  6. How to invoke a function?

    To invoke a function, we follow the function's name with a pair of parentheses (()). For instance, if the function is func, we'll invoke it via func(). Learn more in JavaScript Functions — Creating a function.

  7. What is a function parameter?

    A function parameter is a variable local to a function that holds an argument sent into the function. Learn more in JavaScript Functions — Parameters and arguments.

  8. What is a function argument?

    A function argument refers to a datum that we provide to the function while calling it. Learn more in JavaScript Functions — Parameters and arguments.

  9. In JavaScript, providing more arguments than a function is meant to entertain results in an error. True or false?

  10. In JavaScript, providing less arguments than a function is meant to entertain results in an error. True or false? If false, then what happens with the parameters that aren't provided arguments?

    False. The parameters that aren't provided arguments are set to undefined. Learn more in JavaScript Functions — Parameters and arguments.

  11. What does the return keyword do?

    The return keyword serves to return a value from a function. Learn more in JavaScript Functions — The return keyword.

  12. The return keyword can be used outside functions as well. True or false?

    False. return can only be used inside functions. Learn more in JavaScript Functions — The return keyword.

  13. The return keyword is followed by a statement. True or false? If false, then give the correct description.

    False. The return keyword is followed by an expression. Learn more in JavaScript Functions — The return keyword.

  14. What happens to the code written after a return statement?

    That code is effectively ignored by the JavaScript engine. Learn more in JavaScript Functions — The return keyword.

Operators

  1. What is meant by an operator?

    An operator is simply a symbol or a keyword representing an operation, i.e. a function. Learn more in JavaScript Operators — What are operators?

  2. What is meant by an operation?

    An operation refers to some kind of an internal function in a programming language that is performed over a set of given values. A familiar example is the addition operation in JavaScript, performed by the addition operator (+), which simply adds two numbers together. Learn more in JavaScript Operators — What are operators?

  3. What is meant by an operand?

    An operand refers to the individual values used by an operator. For example, in the expression 1 + 5, the operands of the addition operator (+) are 1 and 5. Learn more in JavaScript Operators — What are operators?

  4. What is meant by a unary, a binary, and a ternary operator?

    A unary operator takes one operand, a binary operator takes two operands, and a ternary operator takes three operands. Learn more in JavaScript Operators — What are operators?

  5. List all the arithmetic operators in JavaScript.

    Addition (+), subtraction (-), multiplication (*), division (/), exponentiation (**), and modulo (%). Learn more in JavaScript Operators — Arithmetic operators.

  6. There is only one string operator in JavaScript. True or false? If true, then which operator is it?

    True. It's the string concatenation (+) operator. Learn more in JavaScript Operators — String operators.

  7. How does JavaScript distinguish between string concatenation and arithmetic addition, given that it's denoted via the same + symbol?

    When either of the operands of + is a string, the operation is taken as string concatenation; otherwise, it's taken to be arithmetic addition. Learn more in JavaScript Operators — String operators.

  8. Name the operator used in the following expression: i++.

    It's called the postfix increment operator. Learn more in JavaScript Operators — Assignment operators.

  9. Name the operator used in the following expression: ++i.

    It's called the prefix increment operator. Learn more in JavaScript Operators — Assignment operators.

  10. How does the expression i++ differ from the expression ++i?

    In i++, the current value of i is returned and then i is incremeted by 1. In ++i, however, first i is incremented and then the resulting value is returned. Learn more in JavaScript Operators — Assignment operators.

  11. What is meant by a relational operator?

    A relational operator determines whether there is a given relationship between two values. Learn more in JavaScript Operators — Relational operators.

  12. What type of a value does a relational operator return?

    A relational operator returns back a Boolean value. Learn more in JavaScript Operators — Relational operators.

  13. List any three relational operators in JavaScript.

    The complete list of relational operators in JavaScript can be found in JavaScript Operators — Relational operators.

  14. What is meant by a logical operator?

    A logical operator either negates the truth value of a given operand or combines together the truth values of two operands together, based on the laws of propositional logic. Learn more in JavaScript Operators — Logical operators.

  15. List the three logical operators in JavaScript.

    We have logical NOT (!), logical OR (||), and finally logical AND (&&). Learn more in JavaScript Operators — Logical operators.

  16. What does it mean when we say that || and && are both short-circuit operators in JavaScript?

    Being 'short-circuit' operators means that both || and && first evaluate the left operand and if that evaluation suggests that there's no point of evaluating the right operand, then they simply just don't do it. Learn more in JavaScript Operators — || and && are short-circuit operators.

  17. What is the ?? operator called and what is its purpose?

    ?? is called the nullish coalescing operator in JavaScript. It's used to specify a fallback value to use when a given value is null (or undefined). Learn more in JavaScript Operators — Nullish coalescing operator.

Scopes

  1. What is meant by the 'scope' of an identifier in JavaScript?

    The scope of an identifier describes its availability in the program. In other words, the scope tells us where the identifier is accessible and where it's not. Learn more in JavaScript Scopes — What are scopes?.

  2. Name the four scopes supported by JavaScript.

    The four scopes are global, local, block and module. Learn more in JavaScript Scopes — What are scopes?.

  3. What does it mean for an identifier to have a global scope?

    It means that the identifier is accessible throughout the entire program. Learn more in JavaScript Scopes — Global scope.

  4. If a let/const declaration isn't part of a function, then the declared identifier is global. True or false?

    False. The let/const declaration might instead be a part of a block and in that case, the underlying identifier won't be global. Learn more in JavaScript Scopes — Global scope.

  5. Suppose x is a global variable. How to access x inside a function foo()?

    In JavaScript, we don't need to do anything special to access a global variable inside a function, unlike in some other languages such as PHP (where we ought to use the global keyword in order to do so).

    Just refer to the variable as it is. Here's an example:

    var x = 10;
    
    function f() {
       console.log(x); // Accessing the global variable x.
    }
    f();
  6. Global identifiers in JavaScript can be accessed across <script> tags. True or false?

    True. An example can be seen in JavaScript Scopes — Global scope.

  7. What happens when we assign to an undeclared variable in JavaScript, supposing that we are in non-strict mode?

    A global variable is created with the given value. An example is demonstrated in JavaScript Scopes — Global scope.

  8. What happens when we assign to an undeclared variable in JavaScript, supposing that we are in strict mode?

    We get an error as it's invalid to assign to an undeclared variable in strict mode. An example is demonstrated in JavaScript Scopes — Global scope.

  9. What does it mean for an identifier to have a local scope?

    It simply means that the identifier can only be accessed inside the function where it's declared. Learn more in JavaScript Scopes — Local scope.

  10. Different functions can define constants with the same name. True or false?

    True. Each function creates its own separate local environment independent from the environment of another function. Hence, we could have identically-named constants in two different functions in JavaScript. Learn more in JavaScript Scopes — Local scope.

  11. JavaScript only supports the idea of lexical scopes, also known as static scopes. What does this mean? Explain using a code snippet.

    This means that the scope of an identifier right away determined by reading the source code, and that it doesn't change with the course of the program. Learn more in JavaScript Scopes — Lexical vs. dynamic scopes.

  12. The scope of an identifier is governed by its declaration, not its assignment. True or false?

    True. It's the declaration of the identifier that matters, not where it's assigned a value. Learn more in JavaScript Scopes — Local scope.

  13. What does it mean when we say that a local variable x of a function f() can shadow a global variable x? Explain using a code snippet.

    It means that the local variable x of f() will effectively shadow, i.e. hide, the global x inside the function.

    Here's an example:

    var x = 'global';
    
    function f() {
       var x = 'local'; // This shadows the global x.
       console.log(x);
    }
    f();
    local

    Learn more in JavaScript Scopes — Identifier shadowing.

  14. What does it mean for an identifier to have a block scope?

    It means that the identifier can only be accessed within the block ({}) it's defined in. Learn more in JavaScript Scopes — Block scope.

  15. Variables declared using var can be made to have a block scope in JavaScript. True or false?

    False. var declarations can never ever exhibit a block scope. Learn more in JavaScript Scopes — Block scope.

  16. What does it mean when we say that a block variable x can shadow a local variable x of a function f()? Explain using a code snippet.

    It means that the block variable x will effectively shadow, i.e. hide, the local variable x of f().

    Here's an example:

    function f() {
       var x = 'local';
       {
          let x = 'block'; // This shadows the local x.
          console.log(x);
       }
       console.log(x);
    }
    f();
    block local

    Learn more in JavaScript Scopes — Block scope.

  17. How does JavaScript resolve names encountered while processing a script?

    The names are searched for in the closest environment where they are referred, and then they are searched for one-by-one in each outer environment until a match is found or if the global environment is reached. At this point, if a match ain't found in the global environment as well, a ReferenceError exception is raised. Learn more in JavaScript Scopes — Name resolution.

Pass By Value

  1. What is meant by 'passing' an identifier in JavaScript?

    Passing an identifier simply means to assign it to some other identifier in JavaScript, which might be by directly assigning it to that identifier, or by providing it as an argument to a function (in which case, it'll land inside the corresponding parameter). Learn more in JavaScript Pass By Value — What is meant by 'passing'?

  2. What is meant by pass-by-value?

    Pass-by-value simply means to pass an identifier by the value that it stores (instead of by its location in the memory). Learn more in JavaScript Pass By Value — What is pass-by-value?

  3. Suppose we have a variable x holding the value 10, and another variable y initialized to the variable x. Will changing x to 20 also cause y to be changed (to 20)? Explain your answer.

    Not at all.

    Here's the code that the question above proposes:

    var x = 10;
    var y = x;
    
    x = 20;
    // Will y change as well?

    Clearly, x and y are both separate variables in memory; changing one won't cause the other to change as well. Learn more in JavaScript Pass By Value — What is pass-by-value?

  4. Objects in JavaScript are passed by reference. True or false?

    False.

    Honestly speaking, it's slightly vague to say that objects in JavaScript are passed by reference. It's much more accurate to say that "objects in JavaScript are passed by value, where the value is a reference".

    JavaScript doesn't support the notion of pass-by-reference in the same sense in which it's supported in other programming languages. Learn more in JavaScript Pass By Value — No pass-by-reference in JavaScript!

  5. Objects in JavaScript are passed by value. True or false?

    True.

    The reality is that objects in JavaScript are passed by value, whereby the value is a reference. Learn more in JavaScript Pass By Value — Passing objects around.

  6. What is meant by a reference in JavaScript?

    A reference simply refers to a value that is used to refer to an object in memory. Objects in JavaScript are NOT stored directly in identifiers; instead, their references are stored.

    Often times, the two terms 'references' and 'objects' are used interchangeably in JavaScript, and that's absolutely fine as long as we remember the thin line between them.

    Learn more in JavaScript Pass By Value — Passing objects around.

  7. Suppose we have a variable x holding the value { a: true }, and another variable y assigned the variable x. Will writing 10 to x.b have any effect on the value stored in y? Explain your answer.

    Absolutely yes.

    Here's the question's statement in the glyphs of code:

    var x = { a: true };
    var y = x;
    
    x.b = 10;
    
    // What will happen to y?

    The variables x and y both hold the exact same object; mutating the object via either of these variables would obviously be seen in the other variable as well, as they just point to the same object.

    Learn more in JavaScript Pass By Value — Passing objects around.

  8. Suppose we have a variable p holding the value { n: 1 }, and another variable q assigned the variable p. Will writing null to p have any effect on the value stored in q? Explain your answer.

    Not at all.

    Here's the question's statement in terms of code:

    var p = { n: 1 };
    var q = p;
    
    p = null;
    
    // What will happen to q?

    The variables p and q both hold the exact same object; mutating the object via either of these variables would be seen in the other variable as well (since it refers to the same object).

    However, changing the value of the variable itself won't have any effect on the other variable. The variables p and q above are NOT tied to one another; assigning null to p would have absolutely no effect on the value stored in q.

    Learn more in JavaScript Pass By Value: No pass-by-reference in JavaScript!

Grammar

  1. What is meant by a statement in JavaScript?

    A statement is a unitary piece of code that denotes a single logical task in a program.

    It's often less precisely said that a statement in JavaScript is any instruction terminated by a semicolon (;). There isn't any necessity for a statement in JavaScript to be terminated by a semicolon, and hence we refrain from defining it this way. Learn more in JavaScript Grammar: What are statements?

  2. There is no such kind of a statement in JavaScript that can contain statements within it. True or false?

    False. In JavaScript, a block statement, denoted via curly braces ({}), serves to contain multiple statements within it.

  3. What is meant by an expression in JavaScript?

    An expression is a piece of code that gets resolved down to a value. Learn more in JavaScript Grammar: What are expressions?

  4. An expression in JavaScript can be comprised of expressions itself. True or false?

    True. For example, the expression 5 + (3 + 5) is comprised to two simpler expressions: 5 and (3 + 5) (which is itself comprised of two expressions: 3 and 5). Learn more in JavaScript Grammar: What are expressions?

  5. What is a primary expression in JavaScript? Give three examples of primary expressions.

    A primary expression is the simplest expression in JavaScript; it doesn't contain any other expression within it. Some examples of primary expressions are: 1, 'Hello', true, undefined, null, and so on. Learn more in JavaScript Grammar: Primary Expressions.

  6. What is a keyword in JavaScript?

    A keyword is a special word that denotes a particular operation. An example is var, which is used to define a new variable in JavaScript. Learn more in JavaScript Grammar: What are keywords?

  7. We can declare a variable in JavaScript called typeof without any error. True or false? Explain your answer.

    No we can't. That's simply because typeof is a keyword and it's invalid to name a variable the same as a keyword in JavaScript.

  8. We can declare a variable in JavaScript called TYPEOF without any error. True or false? Explain your answer.

    Yes we can. Even though typeof is a keyword in JavaScript, TYPEOF has a different case (i.e. it's in all caps) and thus is distinct from typeof.

  9. What is an operator in JavaScript?

    An operator is a symbol or a keyword that denotes a special operation to be carried out on a given value or a set of values. Learn more in JavaScript Grammar: What are operators?

  10. List the three categories of operators in JavaScript along with providing one example for each category.

    The three categories are as follows:

    1. Unary operators, involving just one operand. An example is typeof.
    2. Binary operators, involving two operands. An example is +.
    3. Ternary operators, involving three operands. An example is ?:.

    Learn more in JavaScript Grammar: What are operators?

  11. What is ASI in JavaScript?

    ASI, or Automatic Semicolon Insertion, is a feature of JavaScript whereby semicolons (;) are automatically inserted where there is a need to. Learn more in JavaScript Grammar: Semicolons and ASI.