Skip to content

Instantly share code, notes, and snippets.

@kaniket7209
Last active November 1, 2021 05:07
Show Gist options
  • Save kaniket7209/e4e36c8059287712114924ea290b2e3c to your computer and use it in GitHub Desktop.
Save kaniket7209/e4e36c8059287712114924ea290b2e3c to your computer and use it in GitHub Desktop.
Practical Introduction to JavaScript| JavaScript
1. What is the difference between "==" and "===" operators ?
Answer: The difference between both the comparison operators is that,“==” is used to compare values whereas, “ === “ is used to compare both value and types.
2. Suppose we have a following code below ?
let x = 1;
let y = "1";
console.log(x+y, "---" , x-y);
Why x+y converts it in String and prints 11 and x-y converts that in number and prints 0 ?
Answer: This is because Javascript uses the '+' operator for two things :
i) If JS sees that operands of both the expression are number then it simply performs the addition operation.
ii) If JS sees that the operands of the expressions are of different types then first it converts the number into the string and performs concatenation in both string.
But If Javascript sees that the operator is '-' then it will convert the string into the number then performs normal subtraction to both the numbers.
In general we call the reason in terms of String coercion(for '+') and Type coercion(for '-' ).
3. What is the difference between null and undefined in JavaScript ?
Answer: When a variable is declared but not assigned, then value and type both becomes undefined.
ex- let x; // value of x is undefined
Null represents a non-existent or a invalid value.
ex- let a = null;
4. How can we know the type of JavaScript variable ?
Answer: We can use the "typeof" operator.
5. How can we store different data types in an array in JavaScript ?
Answer: This is because in JavaScript Array has got a dynamic feature comprising arrayList feature from java and non requirement of data types from JavaScript.
1. Is it neccessary to mention datatypes for variables in Javascript ?
a) True
b) False
Answer: b) False
2. What is the default value for any variables in JavaScript ?
a) 0
b) null
c) undefined
d) depends upon the data type provided?
Answer: c) undefined
3. What will happen, if the following JavaScript code is executed?
let a = 10;
a = null;
a = "Hii ";
console.log(a);
a) An error is displayed
B) An exception is thrown
c) print 10
d) print Hii
Answer: d) Hii
4. In JavaScript the a===b statement implies that:
a) Both a and b are equal in value, type and reference address as well.
b) Both are a and b are equal in value only.
c) Both are equal in the value and data type.
d) Both are not same at all.
Answer: c) Both are equal in the value and data type.
5. Which of the following comes under Primitive type ?
a) String
b) Array
b) Functions
d) Character
Answer: a) String
6. Do we need to specify the return type in the function such as static , void or int ?
a) No
b) Yes if we want to return any partcular data type such as String
Answer: No
7. What will happen if the following code will execute ?
function fn(){
let ans = Math.random > 0.5 ? true : "It is less than 0.5" ;
return ans;
}
ans = fn();
console.log(ans);
a) will print true sometime and sometimes will print "It is less than 0.5"
b) throws error of invalid type
c) throws error of invalid syntax type
d) will print only true
Answer: a) will print true sometime and sometimes "It is less than 0.5"
8. Which symbol is used to separate the keys and vaues inside an object ?
a) .
b) ,
c) :
d) ->
Answer: c) :
9. How can we separate key1 : val1 and key2 : val2 of an object ?
a) .
b) ,
c) :
d) ;
Answer: b) ,
10. How can we get the keys(name) of any objects ?
example - let obj = {
name : "Js" ,
age : 10
};
a) loop under objects write obj[iterator that you used in loop]
b) write obj.name
c) just write obj(name)
d) write obj[name]
Answer: b) write obj.name
11. What will be the output for the following code ?
let obj = {
name : "Js" ,
age : 10
};
let key = "age";
console.log(obj[key] , "--" , obj.age);
a) 10--10
b) age--10
c) throws error for syntax[] inside object
d) [10]--[10]
Answer: a) 10--10
12. Can we put different values of different data types in array such as string,integer,boolean all in just a single array?
a) Yes
b) No
Answer: a) Yes
13. Which keyword is used to add elements at first in an array?
a) shift
b) add
c) unshift
d) push
Answer: c) unshift
14. Which keyword is used to add elements at last in an array?
a) shift
b) add
c) unshift
d) push
Answer: d) push
15. Which keyword is used to remove elements from first in an array?
a) pop
b) remove
c) shift
d) delete
Answer: c) shift
16. Which keyword is used to remove elements from last in an array?
a) pop
b) remove
c) shift
d) delete
Answer: a) pop
17. Which keyword is used to get any sections/part of an array ?
a) splice
b) slice
c) substring
d) substr
Answetr: slice
18. Which keyword is used to delete any sections/part of an array ?
a) splice(index1,index2)
b) slice(index1,count)
c) delete(index1,count)
d) splice(index1,count)
where, index is the index1 from where you want to start removing and index2 is the last position where you will stop and count is the number of elements from that index which you want to remove
Answer: d) splice(index1,count)
19. What will be the output for the following code below ?
let a = 3;
let b = "3";
console.log(x+y, "---" , x-y);
a) 33---33
b) 33---0
c) 6---0
d) will print some buffer value
Answer: b) 33---0
20. Execute defferently the following code Block wise and state the result :
let x = 0;
let y = 1;
if(x){ console.log(x) } => Block 1
if(y){ console.log(y) } => Block 2
a) Block 1 will print 0 and Block 2 will print 1
b) Block 1 will not run and Block 2 will print 1
c) Both the blocks will not execute as variables are storing number but condition needed conditional returns.
d) None of the above
Answer: b) Block 1 will not run and Block 2 will print 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment