Introduction to JavaScript
Introduction to JavaScript
Introduction to JavaScript
JavaScript is a lightweight, cross-platform, and interpreted scripting
language. It is well-known for the development of web pages, many
non-browser environments also use it. JavaScript can be used
for Client-side developments as well as Server-side developments.
JavaScript contains a standard library of objects, like Array, Date,
and Math, and a core set of language elements like operators, control
structures, and statements.
Syntax:
<script>
// JavaScript Code
</script>
Example:
• HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Basic Example to Describe JavaScript
</title>
</head>
<body>
</html>
JavaScript is best known for web page development but it is also used
in a variety of non-browser environments.
<!DOCTYPE html>
<html>
<center>
<body>
<h2>External JavaScript</h2>
<p id="demo">Geeks For Geeks.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script src="myScript.js"></script>
</body>
<center>
</html>
JavaScript | Statements
The programming instructions written in a program in a programming
language are known as statements.
Order of execution of Statements is the same as they are written.
1. Semicolons:
• Semicolons separate JavaScript statements.
• Semicolon marks the end of a statement in
javascript.
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Welcome</h2>
<p id="geek"></p>
<script>
// Statement 1
var a, b, c;
// Statement 2
a = 2;
// Statement 3
b = 3;
// Statement 4
c = a + b;
document.getElementById(
"geek").innerHTML =
"The value of c is " + c + ".";
</script>
</body>
</html>
JavaScript | Syntax
JavaScript is a lightweight and dynamic computer programming language. It
is used to create client-side dynamic pages. It is open-source and cross-
platform language.
Basic Syntax:
• Javascript
<script>
document.write("Basic Print method in JavaScript");
</script>
JavaScript syntax refers to the set of rules that determines how JavaScript
programs are constructed:
// Variable declaration
var c, d, e;
<script>
// Function definition
function MyFunction() {
// Function call
MyFunction();
</script>
Output:
Apple
45
JavaScript Operator: JavaScript operators are symbols that are used to
compute the value or in other word we can perform operations on operands.
Arithmetic operators ( +, -, *, / ) are used to compute the value and
Assignment operator ( =, +=, %= ) are used to assign the values to variables.
Example:
• Javascript
<script>
// Variable Declarations
var x, y, sum;
document.write(sum);
</script>
Output:
26
JavaScript Expression: Expression is the combination of values, operators,
and variables. It is used to compute the values.
Example:
• Javascript
<script>
// Variable Declarations
var x, num, sum;
</script>
Output:
10
50
JavaScript Keyword: The keywords are the reserved words that have
special meaning in JavaScript.
// var is the keyword used to define the variable
var a, b;
// function is the keyword which tells the browser to create a
function
function GFG(){};
JavaScript Comments: The comments are ignored by JavaScript compiler.
It increases the readability of code. It adds suggestions, Information and
warning of code. Anything written after double slashes // (single line
comment) or between /* and */ (multi-line comment) is treated as comment
and ignored by JavaScript compiler.
Example:
• Javascript
<script>
// Variable Declarations
var x, num, sum;
// Assign value to the variables
x = 20;
y = 30
document.write(sum);
</script>
50
JavaScript Data Types: JavaScript provides different datatype to hold
different values on variable. JavaScript is a dynamic programming language,
it means do not need to specify the type of variable. There are two types of
data types in JavaScript.
• Primitive data type
• Non-primitive (reference) data type
// It store string data type
var txt = "GeeksforGeeks";
<script>
// Function definition
function func() {
// Declare a variable
var num = 45;
// Function call
func();
</script>
Output:
45