Javascript:-Front-end interview guide

Javascript can be challenging and overwhelming considering the variety of topics to prepare. This guide will help you get your hands on all the areas you need to focus on.

JavaScript is an interesting language to learn but when it comes to an interview lot of the time we do not know how the internal workings of JavaScript code. This is the reason we fail in the interviews. In this article, we will be discussing some of the asked interview questions and trying to answer them.

💡Q 1. What is Primitive data type in JS?

**Ans:-**Primitive data types are used in various ways in JavaScript. We use them to store and manipulate information.They are the simplest and most fundamental types of data that you can work with. These data types represent individual values and have specific characteristics. There are a few main primitive data types in JavaScript.

  1. Number: This represents both integer and floating-point numbers. For example: 5, 3.14.

  2. String: This represents a sequence of characters, like text. For example: 'Hello, World!', 'JavaScript is fun'.

  3. Boolean: This represents a logical value – either true or false. For example: true, false.

  4. Undefined: This represents a variable that has been declared but hasn't been assigned a value yet. For example: let age; (here, age is undefined).

  5. Null: This represents an intentional absence of value. It's often used to indicate that a variable doesn't have a value. For example: let user = null;.

  6. Symbol: This represents a unique and immutable value, often used as object property keys. Symbols are less commonly used compared to other types.

  7. BigInt: This is used for larger integers that can't be represented using the Number type. For example: 10000000000000000000000000000n.

💡Q 2. What is Non-primitive data type in JS?

**Ans:-**Non-primitive data types, also known as reference data types, are a bit more complex. Instead of directly containing the actual data, they store references or addresses to where the data is stored in memory. These data types are more flexible and can hold larger and more complex structures. The main non-primitive data type in JavaScript is the object.

Non-primitive (Reference) Data Types (Object):

  • Object: { name: 'John', age: 30 }

  • Array: [1, 2, 3]

  • Function: function greet() { console.log('Hello!'); }

  • Date: new Date()

  • RegExp: /pattern/

The main differences between primitive and non-primitive data types are that primitives are simple, immutable values directly containing the data, while non-primitives are more complex, store references to data, and allow for more elaborate data structures. Understanding these differences helps developers write efficient, organized, and bug-free code."

💡Q 3. What is difference between null and undefined data types?

Ans:- null and undefined both represent the absence of a value, but they do so in slightly different ways.

  • undefined: Think of this as a variable that's been declared but hasn't been assigned any value yet. It's like an empty box you haven't put anything in yet. For instance, if you declare a variable name but don't give it a value, its value will be undefined.
let name; // This variable is undefined because we haven't assigned a value.
console.log(name); // Output: undefined
  • null: This is more like you deliberately put an empty box there. It's an intentional absence of value. It's like saying, 'Hey, there's nothing in here on purpose.' So, if you set a variable score to null, you're saying that the score is intentionally not available right now.
let score = null; // We intentionally set the score to null.
console.log(score); // Output: null

undefined means something hasn't been assigned a value yet, while null means intentionally no value is assigned.

They're both used to represent absence of value, but undefined often shows up automatically, and null is usually set by a programmer.

💡Q 4.What's the main difference between

== and ===?"

Ans:- "The main difference is in how they handle data types during comparison:

  • == (Equality Operator): This checks if the values on both sides are equal, but it does type coercion. It means if the data types are different, JavaScript will try to convert one or both values so they can be compared.

  • === (Strict Equality Operator): This checks if the values on both sides are equal and also ensures that their data types are the same. It doesn't perform any type of coercion.

console.log(5 == '5'); // Output: true,
console.log(5 === '5'); // Output: false, 

console.log(true == 1); // Output: true, 
console.log(true === 1); // Output: false,

console.log(null == undefined); // Output: true, 
console.log(null === undefined); // Output: false,

💡Q 5. Explain the implicit type coercion in javascript.

**Ans:-**Implicit type coercion is a concept in JavaScript where the language automatically converts one data type to another during certain operations, without you explicitly telling it to do so.

Let's consider adding a number and a string:

let number = 5;
let text = '10';

let result = number + text;
console.log(result); // Output: '510'

In this case, JavaScript implicitly converted the number 5 to a string and then concatenated it with the string '10'. This type of coercion happened automatically behind the scenes."

How can you avoid unintended type coercion:-One way is to use the strict equality operator, ===, which not only checks for equality but also ensures that the data types match. Another approach is to explicitly convert data types using functions like parseInt() or parseFloat() for numbers, or String() for strings. This way, you have more control over the conversion process."

Thank You For Reading🤩

If you like this article, please give thumps up👍 Part-2 soon..............

FRONTEND DEVELOPERINTERVIEW GUIDEJAVASCRIPTWEB DEVELOPMENT
Avatar for Anjali saini

Written by Anjali saini

Hi, my name is Anjali Saini and I'm a creative front-end web developer. I have a passion for creating beautiful, user-friendly websites and applications that are both functional and visually appealing

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.