Skip to content

Latest commit

 

History

History
250 lines (183 loc) · 8.45 KB

1.Basics_Fundamentals-I.md

File metadata and controls

250 lines (183 loc) · 8.45 KB

100 JS Questions (Basics)

1. What is JavaScript? What is the role of JavaScript engine? V.IMP.

  • JavaScript is a programming language that is used to create interactive and dynamic web pages.

  • A JavaScript engine is a program present in web browsers that executes JavaScript code.

image

2. What are client side and server side?

  • A client is a device, application, or software component that requests and consumes services or resources from a server.

    • HTML
    • CSS
    • JavaScript
  • A server is a device, computer, or software application that provides services, resources, or functions to clients.

    • C#
    • PhP
    • Java
    • Python

3. What are variables? What is the difference between var, let, and const? V. IMP.

Feature var let const
Scope Function-scoped Block-scoped Block-scoped
Redeclaration Allowed Not allowed within the same scope Not allowed
Reassignment Allowed Allowed Not allowed
Hoisting Hoisted to the top of the function scope Hoisted to the top of the block scope, but not initialized Hoisted to the top of the block scope, but not initialized
Temporal Dead Zone (TDZ) No Exists before declaration Exists before declaration
function example() {
  var x = 1;
  let y = 2;
  const z = 3;

  if (true) {
    var x = 10; // Redefines x (function-scoped)
    let y = 20; // New y, block-scoped to the if block
    const z = 30; // New z, block-scoped to the if block
    console.log(x, y, z); // Output: 10, 20, 30
  }

  console.log(x, y, z); // Output: 10, 2, 3
}

4. What are some important string operations in JS?

  • substr()

  • indexOf()

  • trim()

  • substring()

  • includes()

  • charAt()

  • replace()

  • slice()

  • valueOf()

  • search()

  • concat()

  • split()

  • toLocaleLowerCase()

  • lastIndexOf()

  • toString()

  • toLocaleUpperCase()

  • charCodeAt()

  • match()

  • Accessing Characters

    charAt(index): Returns the character at a specified index.

    let str = "Hello, world!";
    console.log(str.charAt(0)); // Output: H

    charCodeAt(index): Returns the Unicode value of the character at a specified index.

    console.log(str.charCodeAt(1)); // Output: 101 (Unicode for 'e')
  • Modifying Strings

    concat(str1, str2, ...): Joins strings together.

    let greeting = "Hello";
    let name = "Alice";
    let message = greeting.concat(" ", name, "!");
    console.log(message); // Output: Hello Alice!

    replace(searchValue, newValue): Replaces occurrences of a specified value with another.

    let text = "The quick brown fox jumps over the lazy dog.";
    let newText = text.replace("fox", "cat");
    console.log(newText); // Output: The quick brown cat jumps over the lazy dog.

    toUpperCase(): Converts all characters to uppercase.

    console.log(str.toUpperCase()); // Output: HELLO, WORLD!

    toLowerCase(): Converts all characters to lowercase.

    console.log(str.toLowerCase()); // Output: hello, world!

    trim(): Removes whitespace from both ends of the string.

    let strWithWhitespace = "   Hello, world!   ";
    console.log(strWithWhitespace.trim()); // Output: Hello, world!
  • Extracting Substrings

    slice(startIndex, endIndex): Extracts a portion of a string.

    console.log(str.slice(7, 13)); // Output: world!
    • substring(startIndex, endIndex): Similar to slice, but arguments are treated as unsigned integers.
    • substr(startIndex, length): Extracts a substring based on starting index and length.
  • Checking Properties

    startsWith(searchString, position): Checks if a string starts with a specified substring.

    console.log(str.startsWith("Hello")); // Output: true

    endsWith(searchString, length): Checks if a string ends with a specified substring.

    console.log(str.endsWith("world!")); // Output: true

    includes(searchString, position): Checks if a string contains a specified substring.

    console.log(str.includes("world")); // Output: true
  • Other Useful Methods

    split(separator, limit): Splits a string into an array of substrings.

    let words = str.split(" ");
    console.log(words); // Output: ["Hello,", "world!"]

    indexOf(searchValue, fromIndex): Returns the index of the first occurrence of a specified value.

    console.log(str.indexOf("world")); // Output: 7

    lastIndexOf(searchValue, fromIndex): Returns the index of the last occurrence of a specified value.

    console.log(str.lastIndexOf("o")); // Output: 8

    length: Returns the length of the string.

    console.log(str.length); // Output: 13

5. What is DOM? What is the difference between HTML and DOM?

DOM (Document Object Model) represents the web page as a tree-like structure which allows JavaScript to dynamically access and manipulate the content and structure of a web page.

Feature DOM Virtual DOM
Representation Actual representation of the webpage Lightweight copy of the DOM
Re-rendering Re-renders the entire page Re-renders only the changed parts efficiently
Performance Can be slower, especially with frequent updates Optimized for faster rendering
Use Case Suitable for static websites and simple applications Ideal for dynamic and complex single-page applications with frequent updates

6. What are selectors in JS? V. IMP.

Selectors in JS help to get specific elements from DOM based on IDs, class names, tag names.

DOM Selector methods:

  • getElementById()
  • getElementsByClassName()
  • getElementsByTagName()
  • querySelector()
  • querySelectorAll()

7. What is the difference between getElementById, getElementsByClassName and getElementsByTagName?

Method Description Returns
getElementById(id) Returns the first element with the specified ID. A single element or null if no element is found.
getElementsByClassName(className) Returns a live HTMLCollection of all elements with the specified class name. An HTMLCollection containing elements.
getElementsByTagName(tagName) Returns a live HTMLCollection of all elements with the specified tag name. An HTMLCollection containing elements.

8. What are data types in JS?

A data type determines the type of variable.

Primitive Non-Primitive
Numbers Object
Strings Array
Booleans Function
Undefined Date
Null RegExp
Feature Primitive Data Types Non-primitive Data Types
Types Number, string, Boolean, undefined, null Object, array, function, date, RegExp
Values Can hold only a single value Can hold multiple values and methods
Mutability Immutable (cannot be changed) Mutable (can be changed)
Complexity Simple data types Complex data types

9. What are operators? What are the types of operators in JS?

Operators are symbols or keywords used to perform operations on operands.

Operator Type Operator Example
Arithmetic Operators +, -, *, /, %, ** let x = 5; let y = 2; console.log(x + y);
Assignment Operators =, +=, -=, *=, /=, %=, **= let x = 10; x += 5;
Comparison Operators >, <, >=, <=, ==, !=, ===, !== let x = 5; let y = 3; console.log(x > y);
Logical Operators &&, `
String Operators + (concatenation) let a = 'Hello'; let b = ' World'; var c = a + b;

10. What are the types of conditions statements in JS? V. IMP.

Type of Condition Statement Syntax Example
If/else Statements if (condition) { ... } else if (condition) { ... } else { ... } let x = 5; if (x > 10) { console.log("1"); } else if (x < 5) { console.log("2"); } else { console.log("3"); }
Ternary Operator condition ? expression1 : expression2 let y = 20; let z = y > 10 ? "1" : "0"; console.log(z);
Switch Statement switch (expression) { case value1: ...; break; case value2: ...; break; default: ...; } let a = 5; switch (a) { case 1: console.log("1"); break; case 5: console.log("2"); break; default: console.log("3"); }