Javascript ten interview questions
Published: 2021-05-08 13:25:37

**1. What are truthy and falsy?**
Ans: When we check our value with the condition if fulfill the condition then return this value otherwise return the default value. When we get the first value of the condition that means it's truthy or if we get the default value of the condition this means it's falsy. Some default falsy values that always give you falsy output:- 0, "", undefined, null, Nan, false.
Some Truthy values that always give you truthy output:- " ", true, '0', [], {}.

**2. Differrence between (==) doubal equal and (===) triple equal?**
Ans: Triple equal check the value more strictly then double equal.
Example: ```
let first = 5;
if(first == "5"){
console.log("This is true")
}else{
console.log("This value false")
}
output: This is true
```

```
let second = 5;
if(second === "5"){
console.log("This is true"}
}else{
console.log("This is False")
}
output: This is false
```

**3. What is scope and block scope?**
Ans: Scope means coding accessible area, from where code will be accessible and outside of this area code doesn't access inside of the code. Javascript function, let, const always maintain block scope that means it creates a block area, outside of this block anyone can not be accessed inside of the block.

**4. What is Clouser?**
Ans: Create a function inside another function is called closure. If its function returns or does not return. Every time Calling the function closure return separate private variables. This is a powerful feature of javascript.

**5. What is call, bind & apply?**
Ans: **Call-** Which method you want to use call it and give an argument, after giving the first argument before giving another argument you need to separate them by using a comma.
**Apply-** Which method you want to use call it and give an argument, for giving an argument you need to wrap them into an array.
**Bind-** If you want to use a method repeatedly then you bring your method and bind it with your new object and use it how many times you need it.

**6. What is window & global variable?**
Ans: **Window-** If we want to access any item from anywhere in our javascript code then we can used Window.
**Global Variable-** When we write variables with var that means it is a global variable and we can access it inside function and outside the function.

**7. What is asynchronous, setTimeout, & setInterval?**
Ans: Javascript is a synchronous language means it works part by part one part is done then it starts with another part. But use some methods we able to make javascript asynchronous. From them, one is setTimeout using this method we can set a specific time that means after completing all work you do this job.
Another method setInterval this method pass the function output repeatedly before we don't stop it.

**8. How to find the largest number of an array?**
Ans: To find the largest number we could set an array with let or var variable because its value should change. After that, we could declare another variable that put the max value. This process could run constantly for the searching max number so we can use there a for loop. For loop run one after another and find max number for that, we could set a condition.
Example: 
```
let marks = [40, 55, 33, 79, 12, 98, 29, 59];
let max = marks[0];
for (let i = 0; i < marks.length; i++) {
  let element = marks[i];
  if (element > max) {
    max = element;
  }
}
console.log("Highest marks", max);
```

**9. How to remove a duplicate item from an array?**
Ans: Imagine a chocolate distribution line in kindergarten the distributor calls every student id and gives them chocolates. Some naughty students give their id three to four times. So now we do some code to remove those naughty boys three, four-time id and distribute chocolates equally. For that first, we collect student id and then declare to variable one name id and another uniqueId. uniqueId variable with an empty array because we put their value after removing duplicate id. After that, we declare a loop for doing the work repeatedly and set indexOf for element. Condition set that logic if index equal value -1 that means the value unique and store value in uniqueId.
Example: 
```
let id = [13, 24, 29, 13, 25, 29, 17, 15];
let uniqueId = [];
for (let i = 0; i < id.length; i++) {
    let element = id[i];
    let index = uniqueId.indexOf(element)
    if(index == -1){
        uniqueId.push(element)
    }
}
console.log(uniqueId)
```

**10. How to reverse a string?**
Ans: Suppose you go to the Amazon forest and their people talk an unknown language. After someday, you find that they say a normal sentence reversely. You are a programmer and decide to do some code to understand their language. First, we need to declare a function with the name reverseString with parameter str inside this function we declare a reverse variable that values empty string. After that declare for loop and element var change into char for the reverse process we bring char before reverse and return the reverse value for output. For easy understanding we gave an example:
```
function reverseString(str){
    let reverse = "";
    for (let i = 0; i < str.length; i++) {
        let char = str[i];
        reverse = char + reverse;
        
    }
    return reverse;
}
let statement = "Hello Amazon people how are you? Are you want to learn programming?";
let forAmazon = reverseString(statement);
console.log(forAmazon)
```