JavaScript Basics


What is JavaScript?

JavaScript is a high-level, dynamic, untyped, and interpreted programming language commonly used for client-side web development to create interactive and dynamic web pages.


What are the data types supported in JavaScript?

JavaScript supports the following data types:

  • Primitive Types: Number, String, Boolean, Undefined, Null, Symbol, BigInt
  • Reference Types: Object, Array, Function

What is the difference between == and === in JavaScript?

The == operator checks for value equality with type coercion, while the === operator checks for both value and type equality without type coercion.


console.log(0 == '0');   // true
console.log(0 === '0');  // false

What is a JavaScript variable, and how do you declare one?

A JavaScript variable is a named container that stores data values. You can declare a variable using var, let, or const keywords.


let name = 'John'; // using let
const age = 30;    // using const
var city = 'New York'; // using var

What are the different scopes in JavaScript?

JavaScript has three types of scopes:

  • Global Scope: Variables declared outside any function are in the global scope and can be accessed from anywhere.
  • Function Scope: Variables declared within a function are accessible only within that function.
  • Block Scope: Variables declared with let and const within a block (e.g., within curly braces) are only accessible within that block.

What is a function in JavaScript?

A function is a block of code designed to perform a particular task. It is executed when it is called. Functions can take parameters and return values.


function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet('Alice')); // Hello, Alice!

What are arrow functions in JavaScript?

Arrow functions provide a shorter syntax for writing function expressions. They do not have their own this, making them useful for callbacks.


const add = (a, b) => a + b;
console.log(add(5, 3)); // 8

What is the difference between let, const, and var?

The differences are:

  • var: Function-scoped, can be re-declared and updated.
  • let: Block-scoped, can be updated but not re-declared in the same block.
  • const: Block-scoped, cannot be updated or re-declared, must be initialized at declaration.

What is an object in JavaScript?

An object is a collection of key-value pairs. Each key is a string (or Symbol), and the value can be any data type, including another object.


const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

person.greet(); // Hello, my name is John

What is an array in JavaScript?

An array is a special type of object used to store multiple values in a single variable. Arrays can hold elements of any type and can be manipulated using various methods.


const fruits = ['apple', 'banana', 'orange'];
console.log(fruits[1]); // banana

What is event handling in JavaScript?

Event handling in JavaScript refers to the process of responding to user interactions, such as clicks, key presses, or mouse movements. You can use event listeners to execute code when an event occurs.


document.getElementById('myButton').addEventListener('click', function() {
  alert('Button clicked!');
});

What are template literals in JavaScript?

Template literals are a feature in JavaScript that allow for multi-line strings and string interpolation using backticks (`).


const name = 'Alice';
const greeting = `Hello, ${name}!
Welcome to the site.`;
console.log(greeting);

What is the purpose of the this keyword in JavaScript?

The this keyword refers to the context in which a function is executed. Its value depends on how the function is called, and it can refer to different objects.


How do you create a promise in JavaScript?

A promise is created using the Promise constructor, which takes a function with two parameters: resolve and reject.


const myPromise = new Promise((resolve, reject) => {
  // Asynchronous operation
  const success = true;
  if (success) {
    resolve('Operation successful!');
  } else {
    reject('Operation failed!');
  }
});

How do you handle asynchronous operations in JavaScript?

Asynchronous operations can be handled using callbacks, promises, or the async/await syntax. The async/await syntax allows you to write asynchronous code in a more synchronous manner.


async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

What are higher-order functions in JavaScript?

Higher-order functions are functions that can take other functions as arguments or return them as output. They enable functional programming patterns and callbacks.


function higherOrderFunction(callback) {
  callback();
}

higherOrderFunction(() => console.log('This is a callback function!'));

How do you check if a value is an array in JavaScript?

You can check if a value is an array using the Array.isArray() method.


const values = [1, 2, 3];
console.log(Array.isArray(values)); // true

What is the purpose of the typeof operator in JavaScript?

The typeof operator is used to determine the type of a variable or expression. It returns a string indicating the type of the unevaluated operand.


console.log(typeof 'Hello'); // string
console.log(typeof 123); // number
console.log(typeof true); // boolean
Ads