“Cómo usar typeof en JavaScript” Código de respuesta

Cómo usar typeof en JavaScript

//typeof() will return the type of value in its parameters.
//some examples of types: undefined, NaN, number, string, object, array

//example of a practical usage
if (typeof(value) !== "undefined") {//also, make sure that the type name is a string
  	//execute code
}
The Amateur

tipo de

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number('1') === 'number';      // Number tries to parse things into numbers
typeof Number('shoe') === 'number';   // including values that cannot be type coerced to a number

typeof 42n === 'bigint';

// Strings
typeof '' === 'string';
typeof 'bla' === 'string';
typeof `template literal` === 'string';
typeof '1' === 'string'; // note that a number within a string is still typeof string
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String(1) === 'string'; // String converts anything into a string, safer than toString

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean() will convert values based on if they're truthy or falsy
typeof !!(1) === 'boolean'; // two calls of the ! (logical NOT) operator are equivalent to Boolean()

// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'

// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';

// Objects
typeof {a: 1} === 'object';

// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';
typeof /regex/ === 'object'; // See Regular expressions section for historical results

// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';

// Functions
typeof function() {} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';

// for Date and Regexp objects use instance of
if (dateObj instanceof Date) {}
if (object instanceof RegExp) {}
SubZ390

Cómo usar typeof en JavaScript

console.log(typeof 93);
// Output = "number"

console.log(typeof 'Maximum');
// Output = 'string'

console.log(typeof false);
// Output = "boolean"

console.log(typeof anUndeclaredVariable);
// Output = "undefined"
aMax

typeof JavaScript

var miFuncion = new Function("5+2")
var forma = "redonda"
var tamano = 1
var hoy = new Date()


typeof miFuncion === 'function'
typeof forma === 'string'
typeof tamano === 'number'
typeof hoy === 'object'
typeof noExiste === 'undefined'
Grieving Gharial

JavaScript TypeOf

exports.is = (data) => {
const isArray = Array.isArray(data) && 'array'
const isObject = data == {} && 'object'
const isNull =  data == null && 'null'

const isGrouping =  isArray || isObject || isNull
const isCheck = !isGrouping ? typeof data : isGrouping

const isTypeData = ['number','string','array','symbol','object','undefined','null','function', 'boolean']
const isMatch = isTypeData.indexOf(isCheck)
const isResult = isTypeData[isMatch]
return isResult
}
Restu Wahyu Saputra

typeOf en js

typeof("iAmAString");//This should return 'string'
//NO camelCase, as it is a JS Keyword
gritter97

Respuestas similares a “Cómo usar typeof en JavaScript”

Preguntas similares a “Cómo usar typeof en JavaScript”

Más respuestas relacionadas con “Cómo usar typeof en JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código