JS Interview prac questions

  1. What is the difference between undefined value and null value?
  2. Give example for Math.ceil and Math.floor for 2.5 value.
  3. Define a scope of var, let and const variables.
2 Likes
  1. Undefined = variable has been declared, but its value has not been assigned.
    Null = an empty value or a blank value.

  2. Math.ceil = 3
    Math.floor =2

  3. var = var declarations are globally scoped.
    let = let can be updated but not re-declared
    const =const can neither be updated nor re-declared.

2 Likes

1.The variable which has been assigned as null contains no value. =undefined
null=Null in JavaScript means an empty value and is also a primitive type in JavaScript.

3 .var declarations are globally scoped or function scoped while let and const are block scoped.

1 Like

2.math.celi=3
math.floor=2

1 Like
  1. What is the difference between undefined value and null value?
    Ans-Null-null is a special value meaning “no value”. null is a special object because typeof null returns ‘object’.
    Undefined- undefined means that the variable has not been declared, or has not been given a value.
  2. Give example for Math.ceil and Math.floor for 2.5 value.
    Ans-console.log(Math.ceil(2.5));=3 and Math.floor =2.
  3. Define a scope of var, let and const variables.
    Ans:
    Variables declared with var are in the function scope.
    Variables declared as let are in the block scope.
    Variables declared as const are in the block scope.
2 Likes

Answers:

  1. Undefined: Undefined is like a placeholder till a variable is assigned to a value in its Execution context.actually it is a default behaviour of javascript.
    Null: It is like an empty value, in DSA we can use NULL value to de-referencing memory location by assinging NULL to a variable so that it can loose its memory location (Garbage collection).

2).

  • Math.ceil = 3
  • Math.floor =2

3).Let & const: let and const are stored in a separate scope.
They both have temporal dead zone.

  • Let can be updated but not re-declared
  • const can neither be updated nor re-declared.

var: can be stored in its global scope . since it stores in global scope there is no scope for temporal dead zone for var

@athiras199623

2 Likes

Great work everyone!!! :star_struck: :star_struck:
@manishamahale1720 @eshwararun8 @kumaramansagar01 @kollidayakar77

1 Like

ANS 1:- Undefined means the variable has been declared, but its value has not been assigned.
Null means an empty value or a blank value.
ANS 2:- const value=2.5;
console.log(math.ceil(value));
output is 3
const value2=2.5;
console.log(math.floor(value2));
output is 2.
ANS 3:- var:- 1. It gets hoisted to the top of its scope and initialized undefined.
2. var has the function or global scope.
let:- 1. It also got hoisted to the top of its scope but didn’t initialize.
2. let have the block scope.
const:- 1. It also got hoisted to the top of its scope but didn’t initialize.
2. const has the block scope.

2 Likes

Math. floor(2.5)----go to floor 2.5 which is 2
Math. ceil(2.5)------go to the ceil of 2.5 which is 3

Undefined and null both are falsy values converting them to boolean would result in 0, Both are primitive data types. If we define some variables and don’t assign any values to the variable and then try to use that then undefined would appear, in case of null it is empty like these ---- []," ",

2 Likes