HtmlCssMonk

Basics of JavaScript | Learn basic jS

student, typing, keyboard

DESCRIPTION

Data Types

One of the most fundamental characteristics of a programming language is the set of data types it supports.

There are two types of Data types in JavaScript

  1. Primitive data types
  2. Non-primitive data types

Primitive Data Types are –

Data TypeDescription
Stringrepresents sequence of characters e.g. “hello”
Numberrepresents numeric values e.g. 100
Booleanrepresents boolean value either false or true
Undefinedrepresents undefined value
Nullrepresents null i.e. no value at all

Non-Primitive Data Types are –

Objectrepresents instance through which we can access members
Arrayrepresents group of similar values
RegExprepresents regular expression

JavaScript does not make a distinction between integer values and floating-point values. All numbers in JavaScript are represented as floating-point values.

Variables

Variables can be thought of 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 declare it. This is called Variable declaration. Variables are declared with the var keyword as follows –

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

Storing a value in a variable is called variable initialization. Like this –

<script type="text/javascript">
     var name = "Ali";
     var money;
     money = 2000.50;
</script>

Scope of Variables

The scope of a variable is the region of your program in which it is defined.

JavaScript variables have only two scopes –

  • Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
  • Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code.

Local Variables

A JavaScript local variable is declared inside block or function. It is accessible within the function or block only. For example –

<script>  
function abc(){  
var x=10;//local variable  
}  
</script>  

Global Variables

JavaScript global variable is accessible from any function. A variable i.e. declared outside the function or declared with window object is known as global variable. For example –

<script>  
var data=200;//gloabal variable  
function a(){  
document.writeln(data);  
}  
function b(){  
document.writeln(data);  
}  
a();//calling JavaScript function  
b();  
</script>  

Comments

The JavaScript comments are meaningful way to deliver message. It is used to add information about the code, warnings or suggestions so that end user can easily interpret the code.

The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.

Types of JavaScript Comments

There are two types of comments in JavaScript.

  1. Single-line Comment
  2. Multi-line Comment

Single-Line Comment

It is represented by double forward slashes (//). It can be used before and after the statement.

<script>  
// It is single line comment  
document.write("hello javascript");  
</script>  

Multi-Line Comment

It can be used to add single as well as multi line comments. So, it is more convenient. It is represented by forward slash with asterisk then asterisk with forward slash.

<script>  
/* It is multi line comment.  
It will not be displayed */  
document.write("example of javascript multiline comment");  
</script>  

Next topic – Output in JS

Previous topic – Where to write JS


Leave a Comment

Your email address will not be published. Required fields are marked *