- AConditional block
- BBlock that combines a number of statements into a single compound statement
- CBoth conditional block and a single statement
- DBlock that contains a single statement
Time Taken:
Correct Answer:
Wrong Answer:
Percentage: %
A block of statements is a group of zero or more statements that are treated as a single unit.
In JavaScript, a block of statements is defined using curly braces {}.
A block of statement can be understand as the set of the zero or more statements.
In general, a block of statement has common definition "which combines one or a number of statements into a single statement for ease.
For functions expressed as expressions, the function name is optional in Javascript. A function expression may be defined and used in the same way.
The substr() method extracts a specified number of characters from a string,
starting at a specified index position.
Here's an example of how to use it:
let str = 'Hello World';
let sub = str.substr(6, 5); // Extract 5 characters starting at index 6
console.log(sub); // Output: "World"
The substr() method is similar to the substring() method, which also extracts characters from a string.
The main difference between the two is that substring() does not allow negative indexes, whereas substr() does.
The === operator is known as the strict equality operator in JavaScript.
It is used to test whether two values are equal, and it returns true if the values are equal and their types are the same.
For example:
console.log(1 === 1); // Output: true
console.log('1' === 1); // Output: false
In the first example, the === operator returns true because both operands are equal and have the same type (number).
In the second example, the === operator returns false because although the operands are equal in value, they have different types (string and number).
The strict equality operator is often used as an alternative to the == operator, which is known as the abstract equality operator.
The == operator performs type coercion, which means it converts the operands to the same type before making the comparison.
This can lead to unexpected results in some cases, so the === operator is generally considered a safer choice.
The == operator is known as the abstract equality operator in JavaScript, and it is used to test whether two values are equal.
The operator converts the operands to the same type before making the comparison, so it can return true even if the operands have different types.
For example:
console.log(1 == 1); // Output: true
console.log('1' == 1); // Output: true
In the first example, the == operator returns true because both operands are equal and have the same type (number).
In the second example, the == operator returns true because although the operands have different types (string and number),
they are equal in value after being converted to the same type.
The != operator, on the other hand, is the non-equality operator. It tests whether two values are not equal,
and it returns true if the values are not equal or their types are not the same.
For example:
In JavaScript, if a local variable and a global variable have the same name, the local variable takes precedence over the global variable.
Here's an example of how this works:
let x = 5; // x is a global variable
function foo() {
let x = 10; // x is a local variable
console.log(x); // Output: 10
}
foo(); // Output: 10
console.log(x); // Output: 5
In this example, the local variable x is defined within the foo function, and it takes precedence over the global variable x.
When we log the value of x within the function, it prints 10, which is the value of the local variable.
When we log the value of x outside the function, it
prints 5, which is the value of the global variable.
This is known as variable shadowing,
and it occurs when a local variable with the same name as a global variable "shadows" the global variable, hiding it from view within the local scope.
There are several ways to get a reference to an HTML element in JavaScript:
getElementById():
This method is used to get a reference to an element with a specific id.
For example:
let element = document.getElementById('my-element');
getElementsByClassName():
This method is used to get a list of elements with a specific class name.
For example:
let elements = document.getElementsByClassName('my-class');
The let and var keywords are known as declaration statements or variable declarations.
A declaration statement is a statement in JavaScript that declares a variable or function.
The let and var keywords are used to declare variables in JavaScript.
Here are some examples of declaration statements:
let x; // Declares a variable x with the let keyword
var y; // Declares a variable y with the var keyword
function foo() { // Declares a function foo with the function keyword
// Function body
}
The result of the code is 'object'.
In JavaScript, the typeof operator returns a string that represents the type of a value.
However, the typeof operator has a few quirks,
one of which is that it returns 'object' for null values.
This is a known issue in JavaScript, and it can cause confusion for developers who are not aware
in is not a logical operator in JavaScript.
Logical operators in JavaScript include && (and), || (or), and ! (not). in is an operator that is used to check whether an object has a property with a given name.