Javascript Interview Questions

Q1.What is JavaScript?

A.JavaScript is a scripting language.It is widely used for client-side validation. The JavaScript Interpretor is responsible for translating the JavaScript code to the web browser.

Q2.What are the different data types in JavaScript?

A.Data types in JavaScript are:

  • Primitive Data types
    1. String:It represents a series of characters and is written with quotes. A string can be represented using a single or a double quote.
    2. Number:It represents a number and can be written with or without decimals.
    3. Boolean:It represents a logical entity and can have only two values : true or false. Booleans are generally used for conditional testing.
    4. Undefined:When a variable is declared but not assigned, it has the value of undefined and it’s type is also undefined.
    5. Null:It represents a non-existent or a invalid value.
    6. Symbol:It is a new data type introduced in the ES6 version of javascript. It is used to store an anonymous and unique value.
    7. BigInt:This data type is used to store numbers which are above the limitation of the Number data type. It can store large integers and is represented by adding “n” to an integer literal.
  • Non-Primitive Data types:Primitive data types can store only a single value. To store multiple and complex values, non-primitive data types are used like oblects.

Q3. What are variables?

A. A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local variable and global variable.

Q4. How do we declare variables?

A. There are 3 ways to declare a variable let,var & const.

Q5. Explain let, var & const?

A.let: It allows us to change the value if needed.

let firstName="John"; // a=John
let firstName="John";

firstName=Peter; // firstName="Peter"
const: It doesn't allow to change the value of a variable.
const firstName="John";
  
firstName="Peter"; // error
var:It allows us to change the value if needed.

Q6. What is hoisting?

A. Hoisting is a mechanism in JavaScript that moves the declaration of variables and functions at the top. So, in JavaScript we can use variables and functions before declaring them.

Example1: a=5
     
var a // a=5
Example2:var a
      
a=5 // a=5
Example1 gives the same result as that of Example2.
Note:Only variable get hoisted not its value.

Q7. Explain types of operators used in JavaScript?

Types of operators are:

  1. Arithmetic Operators
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Modulus(Remainder): %
    • Increment: ++
    • Decrement: --
  2. Comparison Operators
    • equal to: ==
    • Identical: ===
    • Not equal to: !=
    • Not Identical: !==
    • Greater than: >
    • Greater than or equal to: >=
    • Less than: <
    • Less than or equal to: <=
  3. Bitwise Operators
    • Bitwise ADD: &
    • Bitwise OR: |
    • Bitwise XOR: ^
    • Bitwise NOT: ~
    • Bitwise left shift: <<
    • Bitwise right shift: >>
    • Bitwise right shift with zero: >>>
  4. Logical Operators
    • Logical AND: &&
    • Logical OR: ||
    • Logical NOT: !
  5. Assignment Operators
    • Assign: =
    • Add & assign: +=
    • Subtract & assign: -=
    • Multiply & assign: *=
    • Divide & assign: /=
    • Modulus & assign: %=
  6. Special Operators
    • Conditional operators: (?:) returns value based on the condition like if-else
    • Comma operators: (,) allows multiple expressions to be evaluated as single statement.
    • new operators: (new) creates an instance (object)
    • type operators: (typeof) checks the type of object.
    • void operators: (void) it discards the expression's return value.

Q8. What is function?

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is only executed when "something" invokes it (calls it)

p { color: red }
var a = 3; //sdf