Book Image

JavaScript and JSON Essentials

By : Sai S Sriparasa
Book Image

JavaScript and JSON Essentials

By: Sai S Sriparasa

Overview of this book

The exchange of data over the Internet has been carried out since its inception. Delimiter-separated lists such as CSV and tag-separated languages such as XML are very popular, yet they are considered to be verbose by a section of developers. JSON (JavaScript Object Notation) is a lightweight text-based code to create objects to transfer data over the Internet. It is a data exchange format that is human-readable (like XML, but without the markup around your actual payload) and its syntax is a subset of the JavaScript language that was standardized in 1999. JavaScript and JSON Essentials is a step-by-step guide that will introduce you to JSON and help you understand how the lightweight JSON data format can be used in different ways either to store data locally or to transfer data over the Internet. This book will teach you how to use JSON effectively with JavaScript. This book begins with a brief refresher course on JavaScript before taking you through how JSON data can be transferred via synchronous, asynchronous, and cross-domain asynchronous HTTP calls. JSON is not just about data transfer; this book throws light on the alternate implementations of JSON as well. You will learn the data types that JavaScript uses and how those data types can be used in JSON. You will go through the concepts of how to create, update, parse, and delete a JSON object. You will also look at the different techniques of loading a JSON file onto a web page, how to use jQuery to traverse through an object, and how to perform access operations. You will also go over a few resources that will make debugging JSON quick and easy.
Table of Contents (15 chapters)

Variables in JavaScript

Now that we have built a Hello World program, let us take the next step and perform a few arithmetic operations on two numbers.


The semi colon (;) is a statement terminator, it tellsthe JavaScript engine that a statement has ended.

Let us take a look at another program, alert_script.html:

The previous program would run and produce four pop-up windows, one after the other, displaying their respective values. A glaring problem here is that we are repetitively using the same numbers in multiple places. If we had to perform these arithmetic operations on a different set of numbers, we would have had to replace them at multiple locations. To avoid this situation, we would assign those numbers to temporary storage locations; these storage locations are often referred to as variables.

The keyword var is used to declare a variable in JavaScript, followed by the name of that variable. The name is then implicitly provided with a piece of computer memory, which we will use throughout the program execution. Let us take a quick look at how variables will make the earlier program more flexible:


Code commenting can be done in two ways: one is single line, and the other is multiline.

Single line comments:

//This program would alert the sum of 5 and 3;

Multiline comments:

/* This program would generate two alerts, the first alert would display the sum of 5 and 3, and the second alert would display the difference of 5 and 3 */

Let us continue with the program:

Now let us alter the value from 5 to 6; the amount of change that we will make here is minimal. We assign the value of 6 to our variable a, and that takes care of the rest of the process; unlike our earlier script where changes were made in multiple locations. This is shown as follows:


Code commenting is a recurring and an extremely important step in the development life cycle of any application. It has to be used to explain any assumptions and/or any dependencies that our code contains.

In JavaScript, we declare a variable by using the keyword var and until a value is assigned to it, the value of the variable will be implicitly set to undefined; that value is overwritten on variable initialization.