What is the difference between null and undefined in JavaScript?

null is a value that represents the intentional absence of any object value, while undefined is a value represents an uninitialized or nonexistent value.

for example in here:

let x = null; // x is intentionally set to null

let y; // y is undefined because it has not been initialized

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

i can check in a variable is an array in JavaScript using the Array.isArray() method.

For example:

let x = [1, 2, 3];

let y = "hello";

console.log(Array.isArray(x)); // true

console.log(Array.isArray(y)); // false

What is the difference between var and let in JavaScript?

The main difference between var and let in JavaScript is that var has function scope, while let has block scope.

they are some example in shown:

function example() {

  var x = 5;

  if (true) {

    var x = 10;

  }

  console.log(x); // logs 10 because x is reassigned within the same function scope

}


function example2() {

  let y = 5;

  if (true) {

    let y = 10;

  }

  console.log(y); // logs 5 because y is only accessible within the block scope of the if statement

}

How do you create a new object in JavaScript?

i can create new object in JavaScript using either object literal notation {}, constructor notation new Object(), or class notation class MyClass {}

for example:

let obj1 = {}; // object literal notation

let obj2 = new Object();

How do you add an element to the end of an array in JavaScript?

an element to the end of an array in JavaScript using the push() method.

for example:

let arr = [1, 2, 3];

arr.push(4)

How do you loop through an object in JavaScript?

let obj = {x: 1, y: 2, z: 3};

for (let prop in obj) {

  console.log(`${prop}: ${obj[prop]}`);

}

// logs "x: 1", "y: 2", "z: 3"

Leave a Reply

Your email address will not be published. Required fields are marked *