100% Practical, Personalized, Classroom Training and Assured Job Book Free Demo Now
App Development
Digital Marketing
Other
Programming Courses
Professional COurses
JavaScript (JS) is the most popular lightweight, interpreted compiled programming language. It can be used for both Client-side as well as Server-side developments. It is also known as a scripting language for web pages. This free JavaScript Tutorial is designed to help both beginners and experienced professionals master the fundamentals of JavaScript and unleash their creativity to build powerful web applications. From basic syntax and data types to advanced topics such as object-oriented programming and DOM manipulation.
JavaScript is a lightweight, cross-platform, single-threaded, and interpreted compiled programming language. It is also known as the scripting language for webpages. It is well-known for the development of web pages, and many non-browser environments also use it.
JavaScript is a weakly typed language (dynamically typed). JavaScript can be used for Client-side developments as well as Server-side developments. JavaScript is both an imperative and declarative type of language. 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.
JavaScript can be added to HTML file in two ways:
Syntax:
<script>
// JavaScript Code
</script>
It was created in 1995 by Brendan Eich while he was an engineer at Netscape. It was originally going to be named LiveScript but was renamed. Unlike most programming languages, JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world. The most common host environment is the browser.
According to a recent survey conducted by Stack Overflow, JavaScript is the most popular language on earth.
With advances in browser technology and JavaScript having moved into the server with Node.js and other frameworks, JavaScript is capable of so much more. Here are a few things that we can do with JavaScript:
JavaScript is considered lightweight due to the fact that it has low CPU usage, is easy to implement, and has a minimalist syntax. Minimalist syntax as in, has no data types. Everything is treated here as an object. It is very easy to learn because of its syntax similar to C++ and Java.
A lightweight language does not consume much of your CPU’s resources. It doesn’t put excess strain on your CPU or RAM. JavaScript runs in the browser even though it has complex paradigms and logic which means it uses fewer resources than other languages. For example, NodeJs, a variation of JavaScript not only performs faster computations but also uses fewer resources than its counterparts such as Dart or Java.
Additionally, when compared with other programming languages, it has fewer in-built libraries or frameworks, contributing as another reason for it being lightweight. However, this brings a drawback in that we need to incorporate external libraries and frameworks.
JavaScript is a modern scripting language that is popular worldwide among developers. It is a lightweight, interpreted compiled language that can be used on both client-side as well as the server side. It was invented in the year 1995 by Brendan Eich. Over the years the language has improved a lot and a lot of new features have been added which make the coding process even easier. This language became an ECMA standard in the year 1997.
In this article, we are going to learn how can we add JavaScript to an HTML document. JavasScript code is inserted between <script> and </script> tags when used in an HTML document. Scripts can be placed inside the body or the head section of an HTML page or inside both the head and body. We can also place JavaScript outside the HTML file which can be linked by specifying its source in the script tag.
These are the basic approaches for doing this:
Table of Content
JavaScript code is placed inside the head section of an HTML page and uses the <script>
element. Insert the <script>
tag between the opening <head>
and closing </head>
tags, and place your JavaScript code inside. This ensures the script is loaded and executed when the page loads.
JavaScript Code is placed inside the body section of an HTML page and in this also we use <script> </script> tag inside and above the closing tag of </body>.
JavaScript can also be used in external files. The file extension of the JavaScript file will be .js. To use an external script put the name of the script file in the src attribute of a script tag. External scripts cannot contain script tags.
We can reference an external script in three ways in javascript:
src = "https://www.geeksforgeek.org/js/script.js"
src = "/js/script.js"
src = "script.js"
JavaScript Syntax is used to define the set of rules to construct a JavaScript code. You need to follow all these so that you can work with JavaScript.
console.log("Basic Print method in JavaScript");
JavaScript syntax refers to the set of rules that determines how JavaScript programs are constructed:
// Variable declaration
let c, d, e;
// Assign value to the variable
c = 5;
// Computer value of variables
d = c;
e = c/d;
These are the features of JavaScript which have some predefined syntax:
Table of Content
A JavaScript variable is the simple name of the storage location where data is stored. There are two types of variables in JavaScript which are listed below:
Example: This example shows the use of Javascript variables.
// Declare a variable and initialize it // Global variable declaration let Name = "Apple" ; // Function definition function MyFunction() { // Local variable declaration let num = 45; // Display the value of Global variable console.log(Name); // Display the value of local variable console.log(num); } // Function call MyFunction(); |
Output:
Apple
45
JavaScript operators are symbols that are used to compute the value or in other words, we can perform operations on operands. Arithmetic operators ( +, -, *, / ) are used to compute the value, and Assignment operators ( =, +=, %= ) are used to assign the values to variables.
Example: This example shows the use of javascript operators.
// Variable Declarations let x, y, sum; // Assign value to the variables x = 3; y = 23; // Use arithmetic operator to // add two numbers sum = x + y; console.log(sum); |
Output:
26
Expression is the combination of values, operators, and variables. It is used to compute the values.
Example: This example shows a JavaScript expression.
// Variable Declarations let x, num, sum; // Assign value to the variables x = 20; y = 30 // Expression to divide a number num = x / 2; // Expression to add two numbers sum = x + y; console.log(num + "<br>" + sum); |
Output:
10
50
The keywords are the reserved words that have special meanings in JavaScript.
// let is the keyword used to
// define the variable
let a, b;
// function is the keyword which tells
// the browser to create a function
function GFG(){};
The comments are ignored by the 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 a comment and ignored by the JavaScript compiler.
Example: This example shows the use of javascript comments.
// Variable Declarations let x, num, sum; // Assign value to the variables x = 20; y = 30 /* Expression to add two numbers */ sum = x + y; console.log(sum); |
Output:
50
JavaScript provides different datatypes to hold different values on variables. JavaScript is a dynamic programming language, which means do not need to specify the type of variable. There are two types of data types in JavaScript.
// It store string data type
let txt = "GeeksforGeeks";
// It store integer data type
let a = 5;
let b = 5;
// It store Boolean data type
(a == b )
// To check Strictly (i.e. Whether the datatypes
// of both variables are same) === is used
(a === b)---> returns true to the console
// It store array data type
let places= [“GFG”, “Computer”, “Hello”];
// It store object data (objects are
// represented in the below way mainly)
let Student = {
firstName:”Johnny”,
lastName:”Diaz”,
age:35,
mark:”blueEYE”}
JavaScript functions are the blocks of code used to perform some particular operations. JavaScript function is executed when something calls it. It calls many times so the function is reusable.
Syntax:
function functionName( par1, par2, ....., parn ) {
// Function code
}
The JavaScript function can contain zero or more arguments.
Example: This example shows the use of Javascript functions.
// Function definition function func() { // Declare a variable let num = 45; // Display the result console.log(num); } // Function call func(); |
Output:
45
The programming instructions written in a program in a programming language are known as statements. The order of execution of Statements is the same as they are written.
Example: In this example, we have shown the use of Semicolons.
let a, b, c; a = 2; b = 3; c = a + b; console.log( "The value of c is " + c + "." ); |
The value of c is 5.
Multiple statements on one line are allowed if they are separated with a semicolon.
a=2;b=3;z=a+b;
JavaScript statements can be grouped together inside curly brackets. Such groups are known as code blocks. The purpose of grouping is to define statements to be executed together.
Example: In this example, we have shown Code Blocks.
function myFunction() { console.log( "Hello" ); console.log( "How are you?" ); } myFunction() |
Hello How are you?
JavaScript ignores multiple white spaces.
Example: In this example, we have shown that JavaScript ignores white spaces.
console.log(10 * 2); console.log(10 * 2); |
20 20
Both the result will be the same
JavaScript code’s preferred line length by most programmers is up to 80 characters. The best place to break a code line in JavaScript, if it doesn’t fit, is after an operator.
Example: In this example, we have shown Line Length and Line Breaks
document.getElementById("geek1").innerHTML =
"Hello Geek!";
Keywords are reserved words and cannot be used as a variable name. A JavaScript keyword tells about what kind of operation it will perform.
Understanding variables and data types in JavaScript will help you to write error-free code. In this article, we are going to learn what are Variables and Datatypes in JavaScript.
In JavaScript, variables are used to store and manage data. They are created using the var
, let
, or const
keyword.
var
:Declares a variable. It has a function-scoped or globally-scoped behavior.
var x = 10;
Example: In this example, we will declare variables using var.
var a = "Hello Geeks" var b = 10; var c = 12; var d = b + c; console.log(a); console.log(b); console.log(c); console.log(d); |
Hello Geeks 10 12 22
let
:Introduces block-scoped variables. It’s commonly used for variables that may change their value.
let y = "Hello";
Example: In this example, we will declare variables using let.
let a = "Hello learners" let b = "joining" ; let c = " 12" ; let d = b + c; console.log(a); console.log(b); console.log(c); console.log(d); |
Hello learners joining 12 joining 12
const
:Declares variables that cannot be reassigned. It’s block-scoped as well.
const PI = 3.14;
Example: In this example, we will declare the variable using the const keyword.
const a = "Hello learners" console.log(a); const b = 400; console.log(b); const c = "12" ; console.log(c); // Can not change a value for a constant //c = "new" //console.log(c) will show error |
Hello learners 400 12
JavaScript is a dynamically typed (also called loosely typed) scripting language. In JavaScript, variables can receive different data types over time. The latest ECMAScript standard defines eight data types Out of which seven data types are Primitive(predefined) and one complex or Non-Primitive.
The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.
Error: Contact form not found.