Book Image

Object-Oriented JavaScript - Third Edition

By : Ved Antani, Stoyan STEFANOV
5 (1)
Book Image

Object-Oriented JavaScript - Third Edition

5 (1)
By: Ved Antani, Stoyan STEFANOV

Overview of this book

JavaScript is an object-oriented programming language that is used for website development. Web pages developed today currently follow a paradigm that has three clearly distinguishable parts: content (HTML), presentation (CSS), and behavior (JavaScript). JavaScript is one important pillar in this paradigm, and is responsible for the running of the web pages. This book will take your JavaScript skills to a new level of sophistication and get you prepared for your journey through professional web development. Updated for ES6, this book covers everything you will need to unleash the power of object-oriented programming in JavaScript while building professional web applications. The book begins with the basics of object-oriented programming in JavaScript and then gradually progresses to cover functions, objects, and prototypes, and how these concepts can be used to make your programs cleaner, more maintainable, faster, and compatible with other programs/libraries. By the end of the book, you will have learned how to incorporate object-oriented programming in your web development workflow to build professional JavaScript applications.
Table of Contents (25 chapters)
Object-Oriented JavaScript - Third Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Built-in Functions
Regular Expressions

Chapter 2, Primitive Data Types, Arrays, Loops, and Conditions


Lets try and solve the following exercises:

Exercises

  1. The result will be as follows:

            > var a; typeof a; 
            "undefined" 
    

    When you declare a variable but do not initialize it with a value, it automatically gets the undefined value. You can also check:

            > a === undefined; 
            true 
    

    The value of v will be:

            > var s = '1s'; s++; 
            NaN 
    

    Adding 1 to the string '1s' returns the string '1s1', which is Not A Number, but the ++ operator should return a number; so it returns the special NaN number.

    The program is as follows:

            > !!"false"; 
            true 
    

    The tricky part of the question is that "false" is a string and all strings are true when cast to Booleans (except the empty string ""). If the question wasn't about the string "false" but the Boolean false instead, the double negation !! returns the same Boolean:

            > !!false; 
            false 
    

    As you'd expect, single negation returns the opposite:

            > !false; 
            true 
            > !true; 
            false 
    

    You can test with any string and it will cast to a Boolean true, except the empty string:

            > !!"hello"; 
            true 
            > !!"0"; 
            true 
            > !!""; 
            false 
    

    The output after executing undefined is as follows:

            > !!undefined; 
            false 
    

    Here undefined is one of the falsy values and it casts to false. You can try with any of the other falsy values, such the empty string "" in the previous example, NaN, or 0.

            > typeof -Infinity; 
            "number" 
    

    The number type includes all numbers, NaN, positive and negative Infinity.

    The output after executing the following is:

            > 10 % "0"; 
            NaN 
    

    The string "0" is cast to the number 0. Division by 0 is Infinity, which has no remainder.

    The output after executing the following is:

            > undefined == null; 
            true 
    

    Comparison with == operator doesn't check the types, but converts the operands; in this case both are falsy values. Strict comparison checks the types too:

            > undefined === null; 
            false 
    

    The following is the code line and its output:

            > false === ""; 
            false 
    

    Strict comparison between different types (in this case Boolean and string) is doomed to fail, no matter what the values are.

    The following is the code line and its output:

            > typeof "2E+2"; 
            "string" 
    

    Anything in quotes is a string, even though:

            > 2E+2; 
            200 
            > typeof 2E+2; 
            "number" 
    

    The following is the code line and its output:

            > a = 3e+3; a++; 
            3000 
    

    3e+3 is 3 with three zeroes, meaning 3000. Then ++ is a post-increment, meaning it returns the old value and then it increments it and assigns it to a. That's why you get the return value 3000 in the console, although a is now 3001:

            > a; 
            3001 
    
  2. The value of v after executing the following is:

            > var v = v || 10; 
            > v; 
            10 
    

    If v has never been declared, it's undefined so this is the same as:

            > var v = undefined || 10; 
            > v; 
            10 
    

    However, if v has already been defined and initialized with a non-falsy value, you'll get the previous value.

            > var v = 100; 
            > var v = v || 10; 
            > v; 
            100 
    

    The second use of var doesn't "reset" the variable.

    If v was already a falsy value (not a 100), the check v || 10 will return 10.

            > var v = 0; 
            > var v = v || 10; 
            > v; 
            10 
    
  3. For printing multiplication tables, perform the following:

            for (var i = 1; i <= 12; i++) { 
              for (var j = 1; j <= 12; j++) { 
                console.log(i + ' * ' + j + ' = ' + i * j); 
              } 
            } 
    

    Or:

            var i = 1, j = 1; 
            while (i <= 12) { 
              while (j <= 12) { 
                console.log(i + ' * ' + j + ' = ' + i * j); 
                j++; 
              } 
              i++; 
              j = 1; 
            }