Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Object-Oriented JavaScript
Object-Oriented JavaScript

Object-Oriented JavaScript: Learn everything you need to know about object-oriented JavaScript (OOJS) , Third Edition

Arrow left icon
Profile Icon Antani Profile Icon Stoyan Stefanov
Arrow right icon
₹799 ₹2919.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (6 Ratings)
eBook Jan 2017 550 pages 3rd Edition
eBook
₹799 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m
Arrow left icon
Profile Icon Antani Profile Icon Stoyan Stefanov
Arrow right icon
₹799 ₹2919.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (6 Ratings)
eBook Jan 2017 550 pages 3rd Edition
eBook
₹799 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m
eBook
₹799 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Object-Oriented JavaScript

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

Before diving into the object-oriented features of JavaScript, let's first take a look at some of the basics. This chapter walks you through the following topics:

  • The primitive data types in JavaScript, such as strings and numbers
  • Arrays
  • Common operators, such as +, -, delete, and typeof
  • Flow control statements, such as loops and if...else conditions

Variables

Variables are used to store data; they are placeholders for concrete values. When writing programs, it's convenient to use variables instead of the actual data as it's much easier to write pi instead of 3.141592653589793; especially when it happens several times inside your program. The data stored in a variable can be changed after it initially assigned, hence the name variable. You can also use variables to store data that is unknown to you while you write the code, such as the result of a later operation.

Using a variable requires the following two steps. You will need to:

  • Declare the variable
  • Initialize it, that is, give it a value

To declare a variable, you will use the var statement like the following piece of code:

    var a; 
    var thisIsAVariable;  
    var _and_this_too;  
    var mix12three; 

For the names of the variables, you can use any combination of letters, numbers, the underscore character, and the dollar sign. However, you can't start with a number...

Operators

Operators take one or two values (or variables), perform an operation, and return a value. Let's check out a simple example of using an operator, just to clarify the terminology:

    > 1 + 2; 
    3 

In the preceding code:

  • The + symbol is the operator
  • The operation is addition
  • The input values are 1 and 2 (they are also called operands)
  • The result value is 3
  • The whole thing is called an expression

Instead of using the values 1 and 2 directly in the expression, you can use variables. You can also use a variable to store the result of the operation as the following example demonstrates:

    > var a = 1; 
    > var b = 2; 
    > a + 1; 
    2 
    > b + 2; 
    4 
    > a + b; 
    3 
    > var c = a + b; 
    > c; 
    3 

The following table lists the basic arithmetic operators:

Operator symbol

Operation

Example

+

Addition

> 1 + 2;   
3   

-

Subtraction

> 99.99 - 11;   
88.99   

*

Multiplication

> 2 * 3;   
6   

/

...

Primitive data types

Any value that you use is of a certain type. In JavaScript, the following are just a few primitive data types:

  1. Number: This includes floating point numbers as well as integers. For example, these values are all numbers-1, 100, 3.14.
  2. String: These consist of any number of characters, for example, a, one, and one 2 three.
  3. Boolean: This can be either true or false.
  4. Undefined: When you try to access a variable that doesn't exist, you get the special value undefined. The same happens when you declare a variable without assigning a value to it yet. JavaScript initializes the variable behind the scenes with the value undefined. The undefined data type can only have one value-the special value undefined.
  5. Null: This is another special data type that can have only one value-the null value. It means no value, an empty value, or nothing. The difference with undefined is that if a variable has a null value, it's still defined; it just so happens that its value is nothing. You...

Primitive data types recap

Let's quickly summarize some of the main points discussed so far:

  • There are five primitive data types in JavaScript:
    • Number
    • String
    • Boolean
    • Undefined
    • Null 

  • Everything that is not a primitive data type is an object.

    The primitive number data type can store positive and negative integers or floats, hexadecimal numbers, octal numbers, exponents, and the special numbers-NaN, Infinity, and -Infinity.

  • The string data type contains characters in quotes. Template literals allow embedding of expressions inside a string.
  • The only values of the Boolean data type are true and false.
  • The only value of the null data type is the null value.
  • The only value of the undefined data type is the undefined value.
  • All values become true when converted to a Boolean, with the exception of the following six falsy values:
    • ""
    • null
    • undefined
    • 0
    • NaN
    • false

Variables


Variables are used to store data; they are placeholders for concrete values. When writing programs, it's convenient to use variables instead of the actual data as it's much easier to write pi instead of 3.141592653589793; especially when it happens several times inside your program. The data stored in a variable can be changed after it initially assigned, hence the name variable. You can also use variables to store data that is unknown to you while you write the code, such as the result of a later operation.

Using a variable requires the following two steps. You will need to:

  • Declare the variable

  • Initialize it, that is, give it a value

To declare a variable, you will use the var statement like the following piece of code:

    var a; 
    var thisIsAVariable;  
    var _and_this_too;  
    var mix12three; 

For the names of the variables, you can use any combination of letters, numbers, the underscore character, and the dollar sign. However, you can't start with a...

Operators


Operators take one or two values (or variables), perform an operation, and return a value. Let's check out a simple example of using an operator, just to clarify the terminology:

    > 1 + 2; 
    3 

In the preceding code:

  • The + symbol is the operator

  • The operation is addition

  • The input values are 1 and 2 (they are also called operands)

  • The result value is 3

  • The whole thing is called an expression

Instead of using the values 1 and 2 directly in the expression, you can use variables. You can also use a variable to store the result of the operation as the following example demonstrates:

    > var a = 1; 
    > var b = 2; 
    > a + 1; 
    2 
    > b + 2; 
    4 
    > a + b; 
    3 
    > var c = a + b; 
    > c; 
    3 

The following table lists the basic arithmetic operators:

Operator symbol

Operation

Example

+

Addition

> 1 + 2;   
3   

-

Subtraction

> 99.99...

Primitive data types


Any value that you use is of a certain type. In JavaScript, the following are just a few primitive data types:

  1. Number: This includes floating point numbers as well as integers. For example, these values are all numbers-1, 100, 3.14.

  2. String: These consist of any number of characters, for example, a, one, and one 2 three.

  3. Boolean: This can be either true or false.

  4. Undefined: When you try to access a variable that doesn't exist, you get the special value undefined. The same happens when you declare a variable without assigning a value to it yet. JavaScript initializes the variable behind the scenes with the value undefined. The undefined data type can only have one value-the special value undefined.

  5. Null: This is another special data type that can have only one value-the null value. It means no value, an empty value, or nothing. The difference with undefined is that if a variable has a null value, it's still defined; it just so happens that its value is nothing. You'll...

Primitive data types recap


Let's quickly summarize some of the main points discussed so far:

  • There are five primitive data types in JavaScript:

    • Number

    • String

    • Boolean

    • Undefined

    • Null 

  • Everything that is not a primitive data type is an object.

    The primitive number data type can store positive and negative integers or floats, hexadecimal numbers, octal numbers, exponents, and the special numbers-NaN, Infinity, and -Infinity.

  • The string data type contains characters in quotes. Template literals allow embedding of expressions inside a string.

  • The only values of the Boolean data type are true and false.

  • The only value of the null data type is the null value.

  • The only value of the undefined data type is the undefined value.

  • All values become true when converted to a Boolean, with the exception of the following six falsy values:

    • ""

    • null

    • undefined

    • 0

    • NaN

    • false

Arrays


Now that you know about the basic primitive data types in JavaScript, it's time to move to a more powerful data structure-the array.

So, what is an array? It's simply a list (a sequence) of values. Instead of using one variable to store one value, you can use one array variable to store any number of values as elements of the array.

To declare a variable that contains an empty array, you can use square brackets with nothing between them, as shown in the following line of code:

    > var a = []; 

To define an array that has three elements, you can write the following line of code:

    > var a = [1, 2, 3]; 

When you simply type the name of the array in the console, you can get the contents of your array:

    > a; 
    [1, 2, 3] 

Now the question is how to access the values stored in these array elements. The elements contained in an array are indexed with consecutive numbers, starting from zero. The first element has index (or position) 0, the second has index...

Left arrow icon Right arrow icon

Key benefits

  • This book has been updated to cover all the new object-oriented features introduced in ECMAScript 6
  • It makes object-oriented programming accessible and understandable to web developers
  • Write better and more maintainable JavaScript code while exploring interactive examples that can be used in your own scripts

Description

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.

Who is this book for?

This book is ideal for new to intermediate JavaScript developers who want to prepare themselves for web development problems solved by object-oriented JavaScript!

What you will learn

  • Apply the basics of object-oriented programming in the JavaScript environment
  • Use a JavaScript Console with complete mastery
  • Make your programs cleaner, faster, and compatible with other programs and libraries
  • Get familiar with Iterators and Generators, the new features added in ES6
  • Find out about ECMAScript 6's Arrow functions, and make them your own
  • Understand objects in Google Chrome developer tools and how to use them
  • Use a mix of prototypal inheritance and copying properties in your workflow
  • Apply reactive programming techniques while coding in JavaScript

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 12, 2017
Length: 550 pages
Edition : 3rd
Language : English
ISBN-13 : 9781785884719
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 12, 2017
Length: 550 pages
Edition : 3rd
Language : English
ISBN-13 : 9781785884719
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 13,258.97
Mastering JavaScript Functional Programming
₹3649.99
Object-Oriented JavaScript
₹3649.99
JavaScript : Functional Programming for JavaScript Developers
₹5958.99
Total 13,258.97 Stars icon
Banner background image

Table of Contents

18 Chapters
1. Object-Oriented JavaScript Chevron down icon Chevron up icon
2. Primitive Data Types, Arrays, Loops, and Conditions Chevron down icon Chevron up icon
3. Functions Chevron down icon Chevron up icon
4. Objects Chevron down icon Chevron up icon
5. ES6 Iterators and Generators Chevron down icon Chevron up icon
6. Prototype Chevron down icon Chevron up icon
7. Inheritance Chevron down icon Chevron up icon
8. Classes and Modules Chevron down icon Chevron up icon
9. Promises and Proxies Chevron down icon Chevron up icon
10. The Browser Environment Chevron down icon Chevron up icon
11. Coding and Design Patterns Chevron down icon Chevron up icon
12. Testing and Debugging Chevron down icon Chevron up icon
13. Reactive Programming and React Chevron down icon Chevron up icon
A. Reserved Words Chevron down icon Chevron up icon
B. Built-in Functions Chevron down icon Chevron up icon
C. Built-in Objects Chevron down icon Chevron up icon
D. Regular Expressions Chevron down icon Chevron up icon
E. Answers to Exercise Questions Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(6 Ratings)
5 star 83.3%
4 star 0%
3 star 0%
2 star 16.7%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Madhur Trivedi Jun 06, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's the best book for the topic.
Amazon Verified review Amazon
Joyce Pretzer Jun 08, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had to return this book. Grandson in prison & books have to be bought from different source.
Amazon Verified review Amazon
Ybsen Perez Dec 30, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I started reading this book with a good JavaScript knowledge basis and really wished to had bought this book before, when I was learning the lenguaje. The authors not only give great explanations for every topic, but also take us to a deeper and deeper journey inside this beautiful language. I read 5 or 6 technical book in a year, and can say It's one of my best books ever. Recommended If you are learning or have a good basis and want to improve your JavaScript programming skills.
Amazon Verified review Amazon
Anthony A. Jun 11, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I enjoy this work because it really helps you grasp OOP in ES6 and beyond. Strong OOP knowledge is very useful for React as well, and understanding how to build a good JS library or framework. This will take you JS game to the next level and help you go behind the scenes.
Amazon Verified review Amazon
Xabier Ayape Jun 29, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Trata Javascript de una forma muy completa y actual.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.