The data being stored in a variable can be categorized in different categories which are stated below.
Variables are holders that can store different information values. Variables are highly useful in situations where you need to use a value in multiple command lines and then need to change that value frequently. Variables can also be used to write processing codes for data to be entered by users like in forms where the user can enter any value but you need to add some processing codes for those values before they are actually entered. = sign is used to assign values to variables. Each of the variables are referred by a unique name in javascript. Here's an example.
<script> name="webtrickshome"; document.write(name); </script>
Any text or numeric characters that appear between the double quotes or single quotes is string. Generally, use of single quotes is much preferred by developers but there are cases where you must use double quotes and cases where we must use single quotes as shown in the example below.
x = "It's webtrickshome"; x = 'It\'s "webtrickshome"';
The string must start with a single or double quote and must be closed by the same. In some cases, the same symbol is needed to use within the string which will truncate the string. To avoid it, we can use \ to convert that symbol in the middle to string as shown in the second example.
Numeric data are written without the single or double quotes because javascript identifies numbers written inside the quotes as strings. Numbers can be with or without a decimal.
x = 30; x = 30.33;
Boolean data can hold only one of the two values, true or false. It's mostly used in conditional statements to carry out one task if the result is true or do something else in case the result value is false.
if(x >= 3){ document.write("x is greater than 2"); }else{ document.write("x is smaller than 3"); }
An undefined data in javascript is a variable with no defined value. Its data type and value are both undefined.
var car;
An empty data in javascript is a variable with value defined as empty. Its data type and value are both legible.
var car = "";
A null data in javascript is something that does not exist. Its datatype and value are both legible too.
var car = NULL;
Arrays are variables that can store more than one value that too of different types too. It makes our work convenient to store huge number of similar type of data into a single variable then display the required one or all in a sequence. Arrays can be writtenin two ways.
var colors = ["red","green","blue",0]; var colors = new Array("red","green","blue",0);
Values from an array can be retrieved by their index number but the thing you need to keep in mind is that the indexing starts from 0 in an array. So, the first value would be retrieved by using arrayName[0] while retrieveing all the data from an array can be done by calling the arrayName itself.
var colors = new Array("red","green","blue",0); document.write(colors[0] + "<br>"); // prints red on the document. document.write(colors); // prints all data from the array on the document separated by ",". // red, green, blue, 0
Objects are another type of data in javascript that can hold multiple values. Each of these values have a unique name which is known as properties. Objects can also store a function as well.
var person = {name:"John", address:"United Kingdom", age:35}
Properties form an object can retrieved by calling the property name along with the object.
document.write(person["address"]); or document.write(person.address);
Data with single value like the string, number, boolean and undefined are grouped as primitive data in javascript while other data types like array, object and null are grouped as complex data. Null and Array are also identified as objects by javascript.
Javascript is more often used to write functions which are not executed directly on page load but instead only get executed when a defined action is performed by the user. The javascript processing codes are placed inside the {} brackets which marks the start and closure of a function.
<button onclick="welcomeText()">Click me</button> <script> function welcomeText(){ document.write("Welcome to Javascript Tutorial!!"); } </script>
Since the command document.write() is placed inside the function welcomeText which is set to be triggered on the event the button is clicked, it won't work until the button is clicked.
Leave a comment