0% found this document useful (0 votes)
5 views3 pages

JavaScript Basics: Questions & Exercises

The document provides practice questions and exercises on JavaScript basics, covering topics such as variables, data types, operators, conditional statements, loops, and functions. Each section includes fundamental questions to test understanding and practical exercises to apply the concepts. This structured approach aids learners in mastering JavaScript programming skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

JavaScript Basics: Questions & Exercises

The document provides practice questions and exercises on JavaScript basics, covering topics such as variables, data types, operators, conditional statements, loops, and functions. Each section includes fundamental questions to test understanding and practical exercises to apply the concepts. This structured approach aids learners in mastering JavaScript programming skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Basics: Practice Questions and Exercises

Variables
Practice Questions:
• 1. What is a variable in JavaScript?
• 2. How do you declare a variable using var, let, and const?
• 3. What is the difference between var, let, and const?
• 4. Can you reassign a value to a const variable?
• 5. What are the rules for naming variables in JavaScript?

Exercises:
• 1. Declare a variable named 'city' and assign your hometown to it.
• 2. Create a variable 'age' using let and update its value later.
• 3. Try to reassign a const variable and observe the error.
• 4. Create three variables using var, let, and const and print their values in the
console.

Data Types
Practice Questions:
• 1. What are the primitive data types in JavaScript?
• 2. How do you check the type of a variable?
• 3. What is the difference between null and undefined?
• 4. What is NaN and how can you check if a value is NaN?
• 5. Explain the difference between == and === operators.

Exercises:
• 1. Create variables of types string, number, boolean, null, and undefined.
• 2. Use typeof operator to check each variable’s type.
• 3. Write a program that compares '5' and 5 using both == and ===.
• 4. Create a variable with NaN value and use isNaN() to check it.

Operators
Practice Questions:
• 1. What are arithmetic operators in JavaScript?
• 2. Explain the difference between == and === operators.
• 3. What is the purpose of the typeof operator?
• 4. What are logical operators in JavaScript?
• 5. What is the difference between ++i and i++?

Exercises:
• 1. Write a program to add, subtract, multiply, and divide two numbers.
• 2. Use logical operators to check if a number is between 10 and 20.
• 3. Demonstrate the difference between pre-increment and post-increment.
• 4. Use ternary operator to print 'Adult' or 'Minor' based on age.

Conditional Statements
Practice Questions:
• 1. What are conditional statements in JavaScript?
• 2. Explain the syntax of if, else if, and else statements.
• 3. What is a ternary operator and how is it used?
• 4. Can you nest if statements in JavaScript?
• 5. How does the switch statement work? Provide an example.

Exercises:
• 1. Write a program to check if a number is positive, negative, or zero.
• 2. Use if...else to determine if a number is even or odd.
• 3. Write a program using switch to print day of the week based on a number
(1-7).
• 4. Use ternary operator to check if a number is greater than 100.

Loops
Practice Questions:
• 1. What are loops used for in JavaScript?
• 2. Explain the difference between for, while, and do...while loops.
• 3. What is the purpose of the break and continue statements?
• 4. How can you iterate over an array using a for loop?
• 5. What is a for...of loop and when should you use it?
Exercises:
• 1. Write a for loop to print numbers 1 to 10.
• 2. Use a while loop to print even numbers between 1 and 20.
• 3. Use a for...of loop to print all elements of an array.
• 4. Write a loop that calculates the sum of numbers from 1 to 100.
• 5. Use break and continue in a loop and observe their behavior.

Functions
Practice Questions:
• 1. What is a function in JavaScript?
• 2. How do you declare and call a function?
• 3. What is the difference between a function declaration and a function
expression?
• 4. What are arrow functions? Give an example.
• 5. What is the purpose of the return statement in a function?

Exercises:
• 1. Write a function that prints 'Hello, World!'.
• 2. Create a function that takes two numbers and returns their sum.
• 3. Write a function to check if a number is prime.
• 4. Convert a normal function to an arrow function.
• 5. Write a function that returns the factorial of a number.

Common questions

Powered by AI

The 'break' statement immediately terminates the loop in which it is included, transferring control to the statement following the loop. This is useful for exiting a loop early when a certain condition is met. The 'continue' statement skips the current iteration of a loop and proceeds with the next iteration, allowing you to avoid certain loop code when specific conditions occur without terminating the loop entirely .

The '==' operator checks for equality between two variables after type coercion, meaning it converts the values to a common type before comparison. Conversely, '===' checks for equality without type conversion, meaning the values and their types must be the same for the expression to return true. Using '===' avoids unexpected results from type coercion, promoting more reliable and predictable code .

'NaN' signifies 'Not-a-Number' and arises when operations that do not yield valid numerical results are performed, often due to dividing zero by zero or converting non-numeric strings to numbers. To check if a value is 'NaN', JavaScript provides the 'isNaN()' function which evaluates whether a value is 'NaN', differentiating it from other numeric values .

Arrow functions streamline syntax and reduce boilerplate, which improves readability by making code more concise, particularly in functional programming patterns. They maintain lexical scope of 'this', simplifying nested functions and callbacks by avoiding context-related errors, which often plague verbose function expressions .

Using 'let' and 'const' provides block-level scoping, which avoids issues like accidental variable reassignment or scope leakage common with 'var', thus enhancing code predictability and maintainability. 'Const' is suited for constants to prevent reassignment and signaling immutability, while 'let' is preferred for variables that require reassignment within specific scopes .

Arrow functions provide a more concise syntax compared to traditional function expressions, using the '=>' notation. They do not bind their own 'this', 'arguments', 'super', or 'new.target', which is useful for preserving the lexical scope of the containing block and avoiding unexpected 'this' behavior in callbacks or methods .

A 'switch' statement evaluates an expression against multiple cases, executing the block of code corresponding to the first match. It is commonly used when a variable needs to be compared with several potential values, allowing for cleaner, more organized code over multiple 'if...else' statements, especially when dealing with numerous options based on the same value .

'Null' is an intentional absence of any value, often assigned by the programmer, while 'undefined' is the default state of uninitialized variables or absent object properties. Knowing when to use each effectively communicates intent in code: 'null' indicates a deliberate 'no value', while 'undefined' signifies unintentional absence or future assignment .

'Var' declares a variable globally or locally to an entire function regardless of block scope, making it less predictable within block-level scopes such as loops. 'Let' limits the scope of the variable to the block, statement, or expression where it is used. 'Const', similar to 'let', is block-scoped but also mandates that the variable must be initialized and cannot be reassigned, making it suitable for constants or immutable data .

A 'for' loop is used when the number of iterations is known beforehand. 'While' loops execute as long as the specified condition is true and the number of iterations isn't known in advance, making them ideal for scenarios where the loop depends on a dynamic condition. 'Do...while' loops execute code once before checking the condition, guaranteeing at least one iteration, which is useful when the code should run at least once regardless of the condition .

You might also like