FAQ Database Discussion Community


Dependent Object Type Check Javascript Seems Redundant

javascript,object,if-statement,typeof
Is there a better way of doing this? If I do this: if(typeof someObject[bar][foo] == "function"){ // Do something } I get a someObject[bar][foo] is not an object error which it's referring to someObject[bar] if bar does not exist. This means that the code assumes you know that someObj[bar] is...

How to get typeof entered value?

javascript,typeof
UPDATE: [SOLVED] Thanks everyone for answering me, I have tried two method and it is really work! Here are jsbin for them: This one was inspired by Secret's answer This one is from Alexander's O Mara's answer I'm trying to check if the entered value is number or not. I'm...

In javascript is there really a good reason to never check for boolean true || false such as if(var){}else{}?

javascript,boolean,typeof
I am fairly certain this isn't a duplicate so please bear with me. I check for boolean true||false using if() as a matter of course in my programming. I've programmed extensively in PHP, some in C# ASP.NET, a bit in Java, a long time ago in C++, & dabled in...

Converting typeof to string

c,gcc,typeof
Is there a way to convert gcc's typeof extension to a string, for example: #define printType(a) printf("%s", #typeof(a)) So that I can do: int a = 4; printf("Type of a is: "); printType(a); And get the output of: Type of a is: int A possible use of this would be...

JavaScript short circuit evaluation error?

javascript,conditional-statements,conditional-operator,typeof,short-circuiting
The two below snippets of JS code have had me confused, in my eyes both should work the same, due to short circuit evaluation. But for some reason snippet '1' causes the error (on the third line): Cannot read property 'match' of undefined Array 'a' holds 3 character values user...

How to declare that a variable has the type of an array element in TypeScript?

typescript,typeof
In TypeScript, it is possible to declare that, e.g., var b has the same type as var s: var s = "hello"; var b: typeof s; Is there a simple way to declare that var b has the same type as the elements in an array? Here, var b should...

Error 1 The type or namespace name * could not be found (are you missing a using directive or an assembly reference?)

c#,generics,enums,typeof
I am trying to make a helper function that return an enum name of any type of enum- working from: https://msdn.microsoft.com/en-us/library/system.enum.getname%28v=vs.110%29.aspx I have run into this error: Error 1 The type or namespace name 'enumType' could not be found (are you missing a using directive or an assembly reference?) From...

How to check the type of a field before checking the value in rethinkdb?

typeof,rethinkdb
I have few tables in rethinkdb with very varied datasets. Mostly because over time, out of simple string properties complex objects were created to be more expressive. When I run a query, I'm making sure that all fields exist, with the hasFields - function. But what if I want to...

Looping through an array of numbers and letters and extract only the numbers?

javascript,string,for-loop,typeof
I would like to loop through an array of numbers and letters and extract only the numbers . I tried using the typeof operator but my code doesn't seems to work str = "55abcdef6ghij78" function NumberAddition(str) { var numArr = []; str = str.split(""); for (var i = 0; i...

In JavaScript, is there any difference between typeof x == 'y' and typeof x === 'y'?

javascript,performance,comparison,undefined,typeof
I'm aware of the difference between strict and loose comparison operators. Clearly x == y is different from x === y. But whenever I see code that uses typeof, it always uses ===. If the typeof operator always evaluates to a string (such as 'boolean', 'number', etc.), then wouldn't typeof...