Javascript Basics Quiz

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.

  1. JavaScript File Extension

    Which file extension is commonly used for JavaScript source files?

    1. .css
    2. .html
    3. .java
    4. .js

    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.

  2. Declaring Variables

    What is one way to declare a variable in JavaScript?

    1. var x = 5;
    2. int x = 5;
    3. let x: number = 5;
    4. x := 5;

    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.

  3. Output to Browser

    Which JavaScript command displays a message in an alert box?

    1. alert('Hello!');
    2. echo('Hello!');
    3. console('Hello!');
    4. prompt('Hello!');

    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.

  4. Single-Line Comments

    How do you write a single-line comment in JavaScript?

    1. <!-- comment -->
    2. # comment
    3. /* comment
    4. // comment

    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.

  5. String Concatenation

    Which operator joins two strings together in JavaScript?

    1. +
    2. *
    3. -
    4. %

    Explanation: The + operator concatenates or joins strings in JavaScript. -, *, and % are used for mathematical operations and do not combine strings.

  6. Strict Equality Comparison

    Which operator checks for both value and type equality in JavaScript?

    1. =
    2. !=
    3. ==
    4. ===

    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.

  7. Basic Data Types

    Which is NOT a basic JavaScript data type?

    1. float
    2. boolean
    3. string
    4. number

    Explanation: float is not a basic JavaScript type; JavaScript uses number for all numeric values. Both string and boolean are fundamental JavaScript data types.

  8. Functions in JavaScript

    How do you declare a simple function named myFunc in JavaScript?

    1. function: myFunc() {}
    2. func myFunc() {}
    3. function myFunc() {}
    4. def myFunc() {}

    Explanation: function myFunc() {} is the correct syntax. func and def are used in other languages; function: is not valid JavaScript.

  9. Executing Code When Page Loads

    Which event can you use to run JavaScript code after a webpage finishes loading?

    1. onsubmit
    2. onload
    3. onclick
    4. onhover

    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.

  10. Accessing HTML Elements

    Which method selects an element by its ID in JavaScript?

    1. document.getElementById()
    2. document.selectById()
    3. document.elementById()
    4. document.getElement()

    Explanation: document.getElementById() is the correct method for selecting elements by ID. The other options are not valid JavaScript DOM methods.