Question

In: Computer Science

How do we return the datatype of arguments without using type of in javascript

How do we return the datatype of arguments without using type of in javascript

Solutions

Expert Solution

The alternative way of typeof operator is Object.prototype.toString.

This is a method that prints out the name of the class that you passed into toString.

JavaScript has seven primitive data types and Object: boolean, null, undefined, number, BigInt, string, symbol and object.

You can use below code to check the data type of each arguments.

Javascript code:-

function checkType(val){
return Object.prototype.toString.call(val).slice(8,-1);
}

console.log(checkType('Test'));
console.log(checkType(1));
console.log(checkType(undefined));
console.log(checkType(true));
console.log(checkType([1,2,3]));
console.log(checkType({}));
console.log(checkType(null));

output:-

  • "String"
  • "Number"
  • "Undefined"
  • "Boolean"
  • "Array"
  • "Object"
  • "Null"

Code Summary:

In above code we created function checkType() that returns a data type of a given arguments and we are printing the output of that using console.log() method.

Note Object.prototype.toString.call() return a result in format [Object object], e.g. [Object Number]. So just to have a Number as output we are slicing the result using slice method.

Output in console:-


Related Solutions

using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
How should we define pornography without inhibiting freedom of expression? How do we enforce such a...
How should we define pornography without inhibiting freedom of expression? How do we enforce such a definition without enforcing censorship?
what type of business information technology do you feel we could live without? why?
what type of business information technology do you feel we could live without? why?
Using the below question and values how do you calculate the standard deviation without using a...
Using the below question and values how do you calculate the standard deviation without using a calculator (by hand) of (s1^2/n1+s2^2/n2) with the standard deviation answer being 1.4938 and 1.1361 /sqrt(1.4938^2/10 + 1.1361^2/10) Choose two competing store and compare the prices of 10 items at both stores. Preform a two-sample hypothesis test to try an prove if one store is more expensive than the other based on your sample. Store A item #1-$5.49, #2 $3.77, #3 $0.87, #4 $3.29, #5...
How do we account for Actual Return(s) on Plan Assets? How does the respective return affect...
How do we account for Actual Return(s) on Plan Assets? How does the respective return affect the Pension Liability?
How do you write JavaScript codes to randomly select five elements by using a single for-loop?...
How do you write JavaScript codes to randomly select five elements by using a single for-loop? How do you display the results to a html page? var password = [“x”, “y”, “z”, “1”, “2”, “3”, “test”]; Sample output: zx21y
Boiler monitor: we will create an app using JQUARY and JavaScript that monitors the temperature and...
Boiler monitor: we will create an app using JQUARY and JavaScript that monitors the temperature and pressure of a boiler. You can model the app based on the Thyroid app. In this chapter, we will create the skeleton of the app, similar to the Thyroid app. Now, just create the pages and the links to navigate between them; you will implement the functionality of the pages in later chapters. The app will have A password-based entry page. A page to...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
What is Mirr? how do you find it using excel without using excel formula? Below is...
What is Mirr? how do you find it using excel without using excel formula? Below is cash flow. I found IRR is 15.59% and required return is 12% CF Y0 Y1 Y2 Y3 Y4 Y5 Y6 Capital spen $ (30,000,000.00) $ 15,062,400.00 Opp cost $   (5,000,000.00) NWC $   (1,120,000.00) $      (150,000.00) $      (170,000.00) $      140,000.00 $   1,300,000.00 OCF $     9,015,600.00 $ 11,392,500.00 $ 12,193,200.00 $ 10,435,800.00 $ (2,640,000.00) $   (3,960,000.00) CF $ (36,120,000.00) $     8,865,600.00 $ 11,222,500.00 $ 12,333,200.00 $...
How do you calculate the average return of a bond per week/month? How do we also...
How do you calculate the average return of a bond per week/month? How do we also get the sharpe ratio once the average return in calculated.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT