JavaScript
JavaScript
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language
keywords, variables, function names, and any other identifiers must always be
typed with a consistent capitalization of letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.
Note:JavaScript ignores spaces, tabs, and newlines that appear in JavaScript
programs
Comments in JavaScript
Any text between a // and the end of a line is treated as a comment and
is ignored by JavaScript.(used for single line comment)
Any text between the characters /* and */ is treated as a comment. This
may span multiple lines. (used for multiline line comment)
JavaScript also recognizes the HTML comment opening sequence <!--.
JavaScript treats this as a single-line comment, just as it does the //
comment.
The HTML comment closing sequence --> is not recognized by JavaScript
so it should be written as //-->.
To display data
JavaScript can "display" data in different ways:
Writing into an HTML element, using innerHTML.
Writing into the HTML output using document.write().
Writing into an alert box, using window.alert().
Writing into the browser console, using console.log().
JavaScript Variables
JavaScript variables are containers for storing data values.
Variables are used to store values (name = "John") or expressions (sum = x + y).
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.
OR
var name;
name = "John";
1.Global Variables
A global variable has global scope which means it can be defined anywhere in
your JavaScript code.
The scope of global variables is throughout the script, it means it is available to
all the functions of a program and can be accessed from anywhere in the
script.
Example.
<html>
<body>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
2.Local Variables
A variable that is declared and used inside the function or block is called a local
variable. Its scope is limited to only that function or block in which it is
declared. It cannot be used outside the block..A local variable will be visible
only within a function where it is defined. Function parameters are always
local to that function.
Example.
<script>
function abc(){
var x=10;//local variable
}
</script>
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.
Example.
<html>
<body onload = checkscope();>
<script type = "text/javascript">
<!--
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar);
}
//-->
</script>
</body>
</html>
OUTPUT:
local
Note: 2015 version of JavaScript allows the use of the const and let keyword to
define a variable.