check data type in javascript
function doSomething(x) {
if(typeof(x) === 'string') {
alert('x is a string')
} else if(typeof(x) === 'number') {
alert('x is a number')
}
}> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
if(typeof bar === 'number') {
//whatever
}typeof("string"); //string
typeof(123); //number//typeof() will return the type of value in it's parameters.
//some examples of types: undefined, NaN, number, string, object, array
//example of a practical usage
if (typeof(value) !== "undefined") {//asuming value is already set
//execute code
}console.log(typeof 42);typeof("iAmAString");//This should return 'string'
//NO camelCase, as it is a JS Keyword
Also in JavaScript:
- crypto.com api
- how to split two digit number in javascript
- go back doesnt load javascript safari
- terminal tree command
- check if object is empty javascript
- create moment duration
- how to put firebase config in a sperate file react native
- next js newsletter
- javascript on hover
- geolocation speed
- fetch multiple urls javascript
- input radio trigger select jquery
- map()
- js remove undefined from object
- convert number to indian rupee format in javascript
- input type number max value validation
- check if 2 strings matches js
- javascript download current html page
- javascript string interpolation
- react redirect after login
- how to return ascending array using for loop in js
- useRef
- w3 js resize event
- javascript switch range