We all know that JavaScript is quite a funny language with tricky parts. Some of them can quickly turn our everyday job into hell, and some of them can make us laugh out loud. Here, I am going to make collection of some javascript weird problems & questions. Which you even can't think about.
Sr. No. | Question |
---|---|
1. |
console.log([ ] === [ ]);
OR
console.log([ ] == [ ]);
returns FALSE.
|
2. |
let a = {'name' : 'Shivam'}
let b = a; b.name = 'Sam'; console.log(a.name); Guess Output : Sam or Shivam?
|
3. |
typeof undefined;
OR
typeof(undefined);
Output : undefined.
|
4. |
typeof null;
OR
typeof(null);
Output : object.
|
5. |
typeof String;
OR
typeof(String);
typeof Number;
OR
typeof(Number);
typeof Object;
OR
typeof(Object);
typeof Function;
OR
typeof(Function);
typeof Boolean;
OR
typeof(Boolean);
Output : Function.
|
6. |
['1', '7', '11'].map(parseInt);
Can you guess the Output?
|
7. |
console.log(0.2 + 0.1 === 0.3);
Can you guess the Output?
|
8. |
console.log(Math.max() > Math.min());
Output : True or False?
|
9. |
console.log(018 - 017);
Can you guess the Output?
|
10. |
Have you ever hear about the "Bang Bang Operator" (!!) in JS?
|
11. |
Write a JS function to take any number of Arguments.
function add(...args) {
let result = 0; for (let arg of args) result += arg; console.log(result); } add(1) // returns 1 add(1,2) // returns 3 add(1, 2, 3, 4, 5) // returns 15 |
12. |
What are Spread Operators?
|
13. |
Does JS have types?
OR
Data Types in Javascript
Some may argue that JS is Untyped or that it shouldn’t call its type system types. It doesn’t
require you to declare a type when making a variable like in some other strongly typed languages i.e
int x = 10.
But, I would argue that JS does have types. JS is both dynamically typed and weakly
typed.
let's discuss more about Static typed & Dynamic typed language.
|
14. |
Statically Typed ~
JS is not statically typed unless you’re using a language, tool such as Typescript or Flow that
compiles to JS code. Statically typed means the type is enforced and won’t change so easily. All
variables must be declared with a type.
int x = 5; string y = 'abc'; |
15. |
Dynamically Typed ~
Dynamically typed languages infer variable types at runtime. This means once your code is run the
compiler/interpreter will see your variable and its value then decide what type it is. The type is
still enforced here, it just decides what the type is.
var a = 1 // int var b = 'test' // string |
16. |
Weakly Typed ~
Weakly typed languages allow types to be inferred as another type. For example,
console.log(1 + '2'); // '12' In JS, it sees you’re trying to add a number with a string — an invalid operation — so it coerces your number into a string and results in the string ‘12’. |
17. |
Primitives Data Types in JS.
There are six types, considered to be Primitives.
A primitive is not an object and has no methods of its own. All primitives are immutable.
Boolean — true or false Null — no value Undefined — a declared variable but hasn’t been given a value Number — integers, floats, etc String — an array of characters i.e words Symbol — a unique value that's not equal to any other value Everything else is an Object type. |
18. | |
19. | |
20. | |
21. |