-4

I'm very new to JavaScript, and I'm keen to learn. What I'm trying to do is play with the if, else statements. I'm trying to validate two types of strings. I can't seem to validate the numbers, and I can not figure it out for the life of me. Any hints would be greatly appreciated please.

JavaScript : typeof example

<script>
    var string1 = 'w3';
    var string2 = (1); 

    if (typeof string1 == 'w3') {
        document.write(string1 + " is a number <br/>");
        else if (typeof string2 == (1) {
            document.write(string2 + "is a number <br/>");
        }
    } else {
        document.write(string2 + " is not a number <br/>");
    }

</script>

grhoades
  • 7
  • 1
  • Both are string types, so `typeof …` evaluates to `'string'` which is truthy and thus coercible to 1. There is no type `w3`. Also, you're missing parentheses and adding unnecessary ones. – Andrew Li Feb 02 '19 at 01:31
  • Thanks a lot Li357, this helps a bunch! – grhoades Feb 02 '19 at 01:39
  • _"I'm trying to figure out how to use if, else, ..."_ is not a question an unlikely to help future readers – Mulan Feb 02 '19 at 02:50

2 Answers2

0

Javascript has only 6 native types: "number", "string", "boolean", "object", "function" and "undefined". The typeof operator tries to figure out the type of a variable. Therefore:

console.log(typeof 'w3');    // will print 'string'
console.log(typeof 1);    //will print 'number'
console.log(typeof true);    //will print 'boolean'
console.log(typeof {});    //will print 'object'
console.log(typeof function(){});    //will print 'function'
console.log(typeof undefined);    //will print 'undefined'

var v1 = 'str';
if (typeof v1 == 'string') {
    console.log('string');
} else {
    console.log('not a string');
}

// and so forth.

Trying playing with this stuff in Chrome's Dev Tools (i.e. Press F12 and type in commands in the console). If you're not familiar, the dev tools thing will change your life if you're coding in Javascript.

Marcio Lucca
  • 370
  • 2
  • 10
0

Currently, you are using the typeof operator to compare a variable to its value. Javascript has six types: number, string, boolean, object, function, and undefined.

Case 1

If you want to see if a variable is a certain type, use typeof, like this:

<script>
    var string1 = 'w3';
    var string2 = 1; 

    if (typeof string1 === 'string') {
        document.write(string1 + " is a number <br/>");
        else if (typeof string2 === 'number') {
            document.write(string2 + "is a number <br/>");
        }
    }
    else {
        document.write(string2 + " is not a number <br/>");
    }

</script>

Case 2

If you want to see if a variable is equal to a value, just use the strict comparison operator(===):

<script>
    var string1 = 'w3';
    var string2 = 1; 

    if (string1 === 'w3') {
        document.write(string1 + " is a number <br/>");
        else if (string2 === 1) {
            document.write(string2 + "is a number <br/>");
        }
    }
    else {
        document.write(string2 + " is not a number <br/>");
    }

</script>

Firstly, it is always better to use the strict equality operator(===) instead of the equality operator(==). You can read more about this here. Secondly, please make your code more legible by using the correct indentation and syntax. This is very important if you want people to be able to read your code easier. Thank you for reading my answer, and good luck!

Vappor Washmade
  • 423
  • 4
  • 14