Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Sunday, December 30, 2018

JavaScript Variable


Hello guy! evening and welcome to my new tutorial post on JavaScript. In my last tutorial i talked about Javascript placment, in this tutorial we will be talking about JavaScript Variables.

JavaScript DataTypes:
One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the types of values that can be represented and manipulated in a programming language.

JavaScript allows you to work with three primitive data types:
  • Numbers eg 123, 150.50 etc.
  • Strings of text eg "this text is string" etc.
  • Boolean eg true or false.
JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.

In addition to these primitive data types, JavaScript support a composite data type known as object. We will talk about an object in detail in a separate tutorial.

You can also read: Javascript Syntax

Note: Java does not make a distinction between integer values and floating-point values. All numbers in JavaScript are represented as floating-point values. JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard.

JavaScript Variables:
Like many other programming languages, JavaScript has variables. Variables can be thought as named containers. You can place data into these containers and then refer to the data simply by naming the container.

Before you use a variable in a JavaScript program, you must first declare it. Variables are declared with the var keyword as follows:

<script  type="text/javascript">
<!--
var money;
var name;
//-->
</script>

You can also declare multiple variables with the same var keyword as follows:

<script  type="text/javascript">
<!--
var money, name;
//-->
</script>

Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or later point when you need that variable as follows:

For instance, you might create a variable named money and assign the value 5000.70 to it later. For another variable you can assign a value the time of initialization as follows:

<script  type="text/javascript">
<!--
var name ="Nova";
var money;
money=5000.70;
//-->
</script>

Note: Use the var keyword only for declaration or initialization. Once for a life of a variable name in a document. You should not re-declare same variable twice.

JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type. Unlike many other languages, you don't have to tell JavaScript during variable declaration what type of value the variable will hold. The value type of a variable can change during the execution of a program and JavaScript takes care of it automatically.

You can also read: How to enable Javascript

JavaScript Variable Scope:
The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

  • Global Variables: A global variable has a global scope which means it is defined every where in your JavaScript code.
  • Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. Below is an example:

<script  type="text/javascript">
<!--
var myVar = "global";  // Global variable declaration
function checkscope()  {
    var myVar = "local";  // Local variable declaration
    document.write(myVar);
}
//-->
</script>

JavaScript Variable Names:
While naming your variables in JavaScript keep the following rules in mind:

  • You should not use any of the JavaScript reserved keyword as a variable name. This keywords are listed out below. For example break or import variable names are invalid.
  • JavaScript variable names should not start with numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid variable name.
  • JavaScript variable names are case sensitive. For example name and Name are two different variables.
JavaScript Reserved Words:
The following are reserved words in javascript. They cannot be used as variable names, functions, methods, loops labels, or any object names.
abstract
boolean
break
byte
case
catch
char
class
const
continue
debugger
default
delete
do
double
else
enum
export
extends
false
final
finally
float
for
function
goto
if
implements
import
in
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
typeof
var
void
volatile
while
with


Alright guys we have come to the end of this tutorial post on JavaScript variable, see you in my next tutorial on JavaScript and thanks for reading. Feel free to drop your questions in the comment box below, bye for now.

Like our facebook page and subscribe with us to get our tutorial posts delivered directly to your emails. Also share this tutorial post on the various social media platforms available.

You can follow us on twitter 
You can follow us on google+ 
You can also follow us on pinterest

Sunday, December 23, 2018

How to add JavaScript to Html (JavaScript placment)


Hello guys! afternoon and welcome to another section of my tutorial post on JavaScript. In my last tutorial i talked about how to enable javascript on various browsers and now we are going to be talking about JavaScript that is how and where to place our JavaScript codes in our Html documents. 

There is a flexibility given to include JavaScript code anywhere in an Html document. But there are following most preferred ways to include JavaScript in your Html file.
  • Script in <head>....</head> section.
  • Script in <body>....</body> section.
  • Script in <body>....</body> and <head>....</head> section.
  • Script in an external file and then include in <head>....</head> section.
In the following section below we will see the various ways in which we can put JavaScript in our Html document.

You can also read: How to enable JavaScript

JavaScript in <head>....</head> section:
If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head as follows:

<html>
<head>
<script  type="text/javascript">
<!--
function sayHello ()  {
     alert("Hello World")
}
//-->
</script>
</head>
<body>
<input  type="button"  onclick="sayHello()"  value="Click" />
</body>
</html>

JavaScript in <body>....</body> section:
If you need a script to run as the page loads so that the script generates content in the page, the script goes to the <body> section of the Html document. In this case you would not have any function defined using JavaScript:

<html>
<head>
</head>
<body>
<script  type="text/javascript">
<!--
document.write("Hello World")
//-->
</script>
<p>This is the body of the document</p>
</body>
</html>

JavaScript in <body> and <head> section:
You can put your JavaScript code in <body> and <head> section altogether as follows:

<html>
<head>
<script  type="text/javascript">
<!--
function sayHello ()  {
     alert("Hello World")
}
//-->
</script>
</head>
<body>
<input  type="button"  onclick="sayHello()"  value="Click" />
</body>
</html>

JavaScript in External File:
As you begin to work extensively with JavaScript, you will likely find out that there are cases where you are reusing identical JavaScript code on multiple pages of a website.

You can also read: JavaScript Syntax

You are not restricted to be maintaining identical code in multiple Html files. The script tag provides a mechanism to allow you store JavaScript in an external file  and then include it into your Html file.

Below is an example to show how to include an external JavaScript file in your Html code using script tag and its src attribute:

<html>
<head>
<script type="text/javascript" src="filename.js" ></script>
</head>
<body>
.............
</body>
</html>

To use JavaScript from an external file source, you need to write your all JavaScript source code in a simple text with extension ".js" and then include that file as shown above.

For example, you can keep following content in filename.js file and then you can use sayHello function in your Html file after including filename.js file:

function sayHello()  {
     alert("Hello World")
}

Alright guys we have come to the end of this tutorial post on JavaScript placement, see you in my next tutorial on JavaScript and thanks for reading. Feel free to drop your questions in the comment box below, bye for now.

Like our facebook page and subscribe with us to get our tutorial posts delivered directly to your emails. Also share this tutorial post on the various social media platforms available.

You can follow us on twitter 
You can follow us on google+ 
You can also follow us on pinterest

Wednesday, December 12, 2018

How to enable JavaScript


Hello guys evening and welcome to my new tutorial post on JavaScript. In this tutorial post we gonna be talking about how to enable JavaScript on our various browsers.

All the modern browsers comes with built-in support for JavaScript. Many at times you may need to enable or disable this support manually.

This tutorial will make you know the procedures in enabling and disabling JavaScript support in browsers : internet explorer, firefox and opera.

You can also read: Media types

JavaScript in Internet Explorer
Here are simple steps to turn on or turn off JavaScript in your Internet Explorer:

  1. Follow Tools-> Internet Options from the menu
  2. Select Security tab from the dialog box
  3. Click the Custom Level button
  4. Scroll down till you find Scripting options
  5. Select Enable radio button under Active scripting
  6. Finally click ok and come out
To disable JavaScript support in your Internet Explorer, you need to select Disable radio button under Active Scripting

You can also read: JavaScript Syntax

JavaScript in Firefox:
Here are simple steps to turn on or turn off JavaScript in your Firefox:

  1. Follow Tool-> Options from the menu
  2. Select Content option from the dialog box
  3. Select Enable JavaScript checkbox
  4. Finally click ok and come out
To disable JavaScript support in your Firefox, you should not select Enable JavaScript checkbox

JavaScript in Opera:
Here are simple steps to turn on or turn off JavaScript in your Opera:

  1. Follow Tools-> Preference from the menu
  2. Select Advanced option from the dialog box
  3. Select Content from the listed items
  4. Select Enable JavaScript checkbox
  5. Finally click ok and come out
To disable JavaScript support in your Opera, you should not select Enable JavaScript support.

You can also read: What is JavaScript?

Warning for Non-JavaScript Browsers
If u have to do some thing important using JavaScript then you can display a warning message to the user using <noscript> tag.

You can add a noscript block immediately after the script block as follows:

<script language="javascript"   type="text/javascript">
<!--
     document.write("Hello World")
//-->

<noscript>
Sorry.....JavaScript is needed to go ahead
</noscript>
</body>
</html>

Now if the user's browser does not support JavaScript or JavaScript is not enabled then message from </noscript> will be enabled on the screen.


Alright guys we have come to the end of this tutorial post on how to enable JavaScript. Always feel free to ask your questions in areas you don't understand properly, and i promise to attend to your questions as soon as possible. 

This blog has a chat box that is meant for interaction, so you guys should always feel free to interact there and share ideas, or you can also interact in the comment box below.

Like our facebook page and subscribe with us to get our tutorial posts delivered directly to your emails. Also share this tutorial post on the various social media platforms available.

You can follow us on twitter 
You can follow us on google+ 
You can also follow us on pinterest  

Friday, December 7, 2018

JavaScript Syntax


Hello guys morning to you all and welcome to this tutorial post on JavaScript. Like you all know i started my tutorial on JavaScript in my previous tutorial post and we looked at a brief JavaScript Overview. Now in this tutorial post am gonna be discussing JavaScript Syntax.

A JavaScript consists of JavaScript statements that are placed within the <Script>.....</Script> HTML tag in the web page.

You can place the <Script> tag containing the JavaScript code anywhere within your web page but it is preferred way to keep it within the <head> tags.

Thursday, December 6, 2018

What is JavaScript?


Hello guys evening and welcome to a brand new section of my tutorials. Its really being a while and i wanna apologize to all my blog readers, have being occupied with so much work lately that i don't even have much time to drop tutorials here for you guys but i promise to make it up to you guys.
     Am happy to inform you guys that i will be starting my tutorials on javascript from this very post. Now lets take a look at the overview.

JavaScript Overview
JavaScript started life as LifeScript, but Netscape changed the name, possibly because of the excitement being generated by Java to JavaScript. JavaScript made its first appearance in Netscape 2.0 in 1995 with a name LiveScript.