Challenge your understanding of fundamental JavaScript concepts with these questions designed for those starting their journey in frontend development. Explore essential topics such as variable declarations, data types, operators, and simple control structures to solidify your programming foundation.
Which keyword is used in JavaScript to declare a variable that can be updated later, as shown in let score = 10?
Explanation: The correct keyword is 'let', which allows variables to be updated after declaration. 'varr' and 'constt' are incorrect as they're misspellings of actual keywords. 'const' is valid in JavaScript but creates variables that cannot be reassigned, while 'declar' is not a JavaScript keyword.
What will be the value of result after executing result = 'Hello' + ' ' + 'World' in JavaScript?
Explanation: The plus (+) operator concatenates strings in JavaScript, so 'Hello' + ' ' + 'World' produces 'Hello World' with a space. 'HelloWorld' omits the space, which the expression does not do. 'Hello+World' would occur only if plus signs were within the strings. 'Hello,World' is incorrect as there are no commas in the concatenation.
If you declare let isActive = true; in JavaScript, what is the data type of isActive?
Explanation: 'boolean' is correct because true and false are Boolean values. 'string' refers to text data, for example 'true' (with quotes). 'number' is for numeric values, not suitable for true. 'null' stands for the absence of any object value, which is different from true or false.
Which symbol is used to multiply two numbers in JavaScript, as in result = 3 * 4?
Explanation: The asterisk (*) is the multiplication operator in JavaScript. The percent sign (%) is used for the modulo operation, not multiplication. The caret (^) is for bitwise XOR, not multiplication, and the hash symbol (#) is not used as an arithmetic operator in JavaScript.
What does the === operator check for in JavaScript?
Explanation: The '===' operator checks if the compared values are equal in both value and type. Using '==' checks only value equality, which may perform type conversion. 'Only type equality' is incorrect, as the operator checks more than just type. 'Assignment' is handled by the '=' operator, not '==='.
Which line correctly declares a basic function named greet in JavaScript?
Explanation: The correct syntax to declare a function is 'function greet() {}'. 'declare greet() {}' is not valid JavaScript syntax. 'func' is not a JavaScript keyword, and 'create function greet[] {}' wrongly uses square brackets and extra text.
Given let colors = ['red', 'green', 'blue'], what is the value of colors[1]?
Explanation: Arrays in JavaScript are zero-indexed, so colors[0] is 'red', and colors[1] is 'green'. 'blue' is at index 2, while 'yellow' isn’t in the array. 'red' is the first element, but the question asks for the second.
What is the correct way to write a simple if statement to check if age is greater than 18 in JavaScript?
Explanation: The 'if (age > 18) {}' syntax is the correct way in JavaScript. 'if age > 18 then {}' is not JavaScript syntax but resembles other programming languages. 'if [age > 18] {}' incorrectly uses square brackets, and 'if (age => 18) {}' incorrectly uses the assignment arrow instead of the greater-than operator.
Which of the following is the correct single-line comment in JavaScript?
Explanation: JavaScript uses double slashes (//) for single-line comments. The hash (#) denotes comments in some other languages but not in JavaScript. '<!-- ... -->' is for HTML comments. Double dash (--) is not comment syntax in JavaScript.
If you declare let value; without assigning anything, what is the value of value?
Explanation: A variable declared without assignment automatically gets the value 'undefined' in JavaScript. 'null' is a specific assigned value indicating no value, not the default. 'empty' is not a data type or value in JavaScript, and 'zero' (0) is a numeric value, not the default for uninitialized variables.