Explore easy questions covering fundamental JavaScript concepts, suitable for beginners preparing for interviews or assessments. Each question tests key areas of the language using realistic examples.
Which file extension is commonly used for JavaScript source files?
Explanation: .js is the standard file extension for JavaScript code files. .java is used for Java, .css for Cascading Style Sheets, and .html for HTML files, none of which contain standard JavaScript source code.
What is one way to declare a variable in JavaScript?
Explanation: var x = 5; is valid JavaScript syntax for declaring a variable. x := 5; is from other languages like Go. int x = 5; uses syntax not found in JavaScript. let x: number = 5; is TypeScript, not standard JavaScript.
Which JavaScript command displays a message in an alert box?
Explanation: alert('Hello!'); creates a popup alert in the browser. prompt('Hello!'); asks for input instead. console('Hello!'); is not a valid command. echo('Hello!'); is used in PHP, not JavaScript.
How do you write a single-line comment in JavaScript?
Explanation: // comment is the correct way to write single-line comments in JavaScript. <!-- comment --> is for HTML. # comment is used in Python and shell scripts. /* comment is the start of a multi-line comment in JavaScript.
Which operator joins two strings together in JavaScript?
Explanation: The + operator concatenates or joins strings in JavaScript. -, *, and % are used for mathematical operations and do not combine strings.
Which operator checks for both value and type equality in JavaScript?
Explanation: === checks both value and type, ensuring strict equality. == only checks value. = is an assignment operator. != checks if values are not equal but is not strict.
Which is NOT a basic JavaScript data type?
Explanation: float is not a basic JavaScript type; JavaScript uses number for all numeric values. Both string and boolean are fundamental JavaScript data types.
How do you declare a simple function named myFunc in JavaScript?
Explanation: function myFunc() {} is the correct syntax. func and def are used in other languages; function: is not valid JavaScript.
Which event can you use to run JavaScript code after a webpage finishes loading?
Explanation: onload triggers code when the page has finished loading. onclick is for clicks, onhover is not a standard event, and onsubmit is for form submission.
Which method selects an element by its ID in JavaScript?
Explanation: document.getElementById() is the correct method for selecting elements by ID. The other options are not valid JavaScript DOM methods.