-1

All indicators is that this code should work. It does not. Check it out:

aString = "Why doesn't this work?";

if ( typeof aString == String) {
alert("I am a string!!!");
}

Only if I add quotations and make string minuscule, does it work.

aString = "This does work, but why?";

if ( typeof aString == 'string') {
alert("I am a string!!!");
}
ironSteel
  • 41
  • 5
  • 1
    `String` is an object, `typeof` returns an actual _string_ with the type. Are you thinking of `instanceof`? – VLAZ Sep 27 '16 at 09:51
  • `typeof` returns string name of var type – Maxx Sep 27 '16 at 09:52
  • Possible duplicate of [Which is best to use: typeof or instanceof?](http://stackoverflow.com/questions/899574/which-is-best-to-use-typeof-or-instanceof) – VLAZ Sep 27 '16 at 09:52
  • Even if I take out typeof, it still does not work. Why does the console not recognize aString being == to String? Is the String object different from an actual "String" i.e. "this is a string." – ironSteel Sep 27 '16 at 09:54
  • Read about [`typeof`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/typeof), _The typeof operator returns a string indicating the type of the unevaluated operand._ And [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), _The String global object is a constructor for strings, or a sequence of characters._ – Rayon Sep 27 '16 at 09:56
  • So why does aString == "string" come back as true? Is that how you refer to strings in javascript? As something inside of a quotation? It is the same for number. – ironSteel Sep 27 '16 at 09:58
  • As @Maxx has stated it returns the name as a string of the type you're checking so it would return `'string'` which is `== 'string'`. Would `if (typeof aString == typeof String)` be a better comparison? – Geoff James Sep 27 '16 at 09:59
  • Yes, `String` is not the text that says "string". You are comparing text to _something else_. The two won't necessarily be the same. `typeof` tells you "this is of type X" and you are trying to compare this statement with a random variable. It's like asking "why is this not the same `str = "bob"; bob = "Robert"; str == Bob`?" - it's completely different things and the value of one _happens_ to be the variable name of another. You aren't comparing varible names, however, but contents. – VLAZ Sep 27 '16 at 09:59
  • `So why does aString == "string" come back as true` it doesn't `typeof aString` returns the literal text `"string"`. This is exactly the same as the text `"string"`. Therefore, the condition returns `true`. – VLAZ Sep 27 '16 at 10:01
  • @vlaz How would I check for a string without using 'instanceof' or 'typeof' so that I could make a direct comparison inside of the conditional? – ironSteel Sep 27 '16 at 10:02
  • @ironSteel why would you want to do it without `typeof`? – VLAZ Sep 27 '16 at 10:04
  • @vlaz Because I am looking for something direct that represents a string. I thought that was String, but as others have pointed out, it is an object constructor for a string. So I am wondering what in the JS language, represents an actual string, so that I can use it inside of a conditional. – ironSteel Sep 27 '16 at 10:06
  • @ironSteel yes, but what problem does _not_ using `typeof` solve? Why do you want to avoid using that? The answer is that if you use the plain value you can't. Because it's just a value. You have to somehow work out _what_ the _type_ of the value is and `typeof` is the easiest, safest and most convenient way to do it. It's literally build for that reason. Again, why would you want to do it in a different way? – VLAZ Sep 27 '16 at 10:08
  • @vlaz Because I thought that there was a value inside of the JS language that represented all strings, and all numbers. If I use typeof, it has to check first to see if it is a string. I thought I could bypass that, and simply contrast it directly with JavaScript's representation of all strings. – ironSteel Sep 27 '16 at 10:11
  • @ironSteel think about it, you still only have a value when you're handling a variable. `a = 1`, `b = "2"`, `c = [3]` would hold a number, a string and an array respectively but when you compare them, you are only checking the values they hold. There is nothing in any language I know of that will allow you to do `a == "string"` because that is always a value comparison. If you want a type check, you always need an extra mechanism - a language operator like `typeof/instanceof`, a function like `isString` or, for strongly-typed languages, a compile-time check will flag `b == 7` as invalid. – VLAZ Sep 27 '16 at 10:22

5 Answers5

0
var a = 'str';
console.log(typeof a);

Here typeof a returns 'string' not String.String is a constructor function. That's why in first case it does not work.

ninjawarrior
  • 328
  • 1
  • 7
0

That is because typeof actually returns a string stating the type.

So you have to compare it to a string as well, such as "string".

Be careful of the following

 var myString = new String('Hallo what's up');

 console.log(typeof myString); // Object
JHolub
  • 290
  • 3
  • 15
0

Run below two codes in browser console and you will know the difference.

  var abc = 'New string'
  console.log(abc)

  var def = new String('Another string')
  console.log(def);

You are comparing a string with its constructor function.

Read about primitive string value and String object.

Bhaskar Gyan Vardhan
  • 9,057
  • 2
  • 18
  • 18
0

aString = "Why doesn't this work?";

if ( typeof aString === String) {
    console.log("I am a string, but typeof returns a string and not String constructor!!!");
}

aString = "Why does this work?";

if ( typeof aString === 'string') {
    console.log("aString is a string and typeof aString returns 'string'!!!");
}

console.log("A string is not equal to its constructor->",'string' !== String);
console.log("Any strings constructor is equal to String constructor->",'string'.constructor === String);
console.log("This is not useful in typeof as all results are strings even if it is number or boolean ->",(typeof 43).constructor === String);

console.log("The useful comparison would be ->",typeof 43 === "number");

Table for return values of typeof

typeof "any string" === "string";//true
typeof new String('hey') === "object";//true
Object.prototype.toString.call(new String('hey')) === "[object String]";//true
typeof (new String('hey')).valueOf() === "string";//true
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • console.log("Any strings constructor is equal to String constructor->",'string'.constructor === String);" Can you give some examples of string constructors? Are these constructors that the programmer himself can make outside of using the String constructor? – ironSteel Sep 27 '16 at 10:44
  • No, not something programmer himself can make. You can do `"hey".length` or `"hey".charAt(1)` ,this is possible with a global [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) that holds these properties and functions. `String` indeed referee to this globally available `String`. `constructor` is just a means to refer to this `String` from an instance of `String`. Read [this chapter](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch1.md) there is a lot more to be explained in a comment! – sabithpocker Sep 27 '16 at 11:08
  • Thanks I will check that out. Vlaz was saying up top that in a programming language, you will always have to use a 'typeof" object to check what kind of value you are holding. I thought that there was a value in JS that would do that for you automatically. I guess that does not exist, and String is just a global object constructor of strings. There is no "String" that universally checks for all strings in the JS language... – ironSteel Sep 27 '16 at 11:14
  • There are two ways for a string to exist. In its literal form `"hey"` and its object form `new String('hey')` that is rarely used and happens only if we explicitly do it. All literals obey `typeof` giving `"string"` which is sufficient for almost all cases. If you have an edge case with `new String` being used you can use `Object.prototype.toString.call(testString)` that returns `"[object String]"` for both literals and String objects. – sabithpocker Sep 27 '16 at 11:31
  • To answer your question, if you don't use `new String` anywhere, which is the common case, you can use `typeof` for your literals and should work fine. – sabithpocker Sep 27 '16 at 11:34
0

Only the second version of your code is correct because typeof is intended to return the type of an object as a string representation. When you use typeof on a String you receive 'string'.

You can read the details in the ECMAScript® 2016 Language Specification.

fxdapokalypse
  • 21
  • 1
  • 3