Thursday 5 May 2016

Comment and declare function and variable in JavaScript

How to comment in JavaScript and Which type of comment used in JavaScript?

JavaScript support both C style comment and c++ style comment.

  • Any text write and before write "//" comment it known as a "End Of Line" Comment and is ignored by JavaScript.
  • Any text between the characters "/*" and "*/" is treated as a comment. and this may ignored multiple line by JavaScript.
  • JavaScript also identify the HTML comment opening sequence "" is not identify by JavaScript so it should be written as a "//-->".

Example
















=> How to declare a function in JScript?



Jscript also provide a in-built function like

  • Alert
  • Confirm
  • Prompt


User Defined Function

Write a function name for block of code and call when you need it.
You define a function in Head section of the web page.
"function" keyword is used to declare a function name and any argument.

Syntax
\
function  functionName(argument)

           statements
}

Example



















=> How to declare a variable in JScript?



Developer use a Variable to store a Variable.
A Variable can hold several types of data.
In JScript  we don't have to declare a datatype before using it.
Any Variable hold any JScript datatype like String, Numeric, Boolean Values, etc.

There are several rules for declaring the variable.

  • The variable  name must start with a letter or an underscore. 
  • You can use numbers in a variable  name, but not as the first character. 
  • You can't use space to separate characters. 
  • Capitalize the first letter of every  word except the first .

Syntax

To declare a Variable we can use a "var" keyword.

var fname

To assign values to Variables, add an equal sign and the value.

var fname="sandy"
var n=1

No comments:

Post a Comment