Introduction
JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment (for example, a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.
JavaScript was initially created to “make web pages alive”. Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.
Different JavaScript Engines Available:
- Spider Monkey (Firefox)
- V8 (Chrome, Opera & Edge)
- JavaScriptCore (Safari)
- Chakra (Internet Explorer)
What makes JavaScript unique
There are at least three great things about JavaScript:
- Full integration with HTML/CSS.
- Simple things are done simply.
- Support by all major browsers and enabled by default.
Developer Console
To see errors and get a lot of other useful information about scripts, “developer tools” have been embedded in browsers.
Most developers lean towards Chrome or Firefox for development because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing “catch-up” to Chrome or Firefox. So most developers have a “favorite” browser and switch to others if a problem is browser-specific.
In most browsers following are the three common shortcuts to open Console:
- F12
- Ctrl + Shift + I
- Ctrl + Shift + J
Hello World
We'll run the scripts on Browser. So, create a html(index.html) & JavaScript(main.js) files.
Write the following code in main.js:
console.log("Hello, World!");
This code will print "Hello World!" onto the Console
Now, attach the JavaScript file to html
<script src="./main.js"></script>
Variables
You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Declaring Variables
You can declare a variable in following ways:
Simply Assigning Value:
x = 22;
This syntax can creates Global Variables. This way of variable creation should always be avoided.
With the keyword var
var x = 22;
This syntax can creates both Global & Local Variables. Now, the var keyword is not used/recommended due to it's global & hosting characteristics.
With the keyword let
let x = 22;
This syntax can be used to declare a block scope local variable. Recommended over var.
With the keyword const
const x = 22;
This syntax can be used to declare a block scope local Constant variable. Recommended over var & should be used where possible.
Variable Scope
When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.
JavaScript before ECMAScript 2015 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log 5, because the scope of x is the function (or global context) within which x is declared, not the block, which in this case is an if statement.
if(true) {
var x = 5;
}
console.log(x);
This behavior changes, when using the let declaration introduced in ECMAScript 2015.
if(true) {
let x = 5;
}
console.log(x);
Data types
The latest ECMAScript standard defines seven data types:
-
Primitives:
- Boolean. true & false
- null. A special keyword denoting a null value.
- undefined. A top-level property whose value is undefined
- Number. like: 22 or 10.25, etc
- String. "Collection of Characters"
- Symbol (new in ECMAScript 2015). A data types whose instances are unique & immutable
- Object
Although these data types are a relatively small amount, they enable you to perform useful functions with your applications. Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
if else statement
Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows:
if (condition) {
statement_1;
} else {
statement_2;
}
condition can be any expression that evaluates to true or false. See Boolean for an explanation of what evaluates to true and false. If condition evaluates to true, statement_1 is executed; otherwise, statement_2 is executed. statement_1 and statement_2 can be any statement, including further nested if statements.
You may also compound the statements using else if to have multiple conditions tested in sequence, as follows:
if(condition_1) {
statement_1;
} else if(condition_2) {
statement_2;
} else if(condition_n) {
statement_n;
} else {
statement_last;
}
while statement
A while statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:
while(condition) {
statements;
}
If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.
The condition test occurs before statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again. If the condition returns false, execution stops and control is passed to the statement following while.
Function Declarations
A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:
- The name of Function.
- A list of arguments to the function, enclosed in parentheses and separated by commas.
- The JavaScript statements that define the function, enclosed in curly brackets, { }.
For example, the following code defines a simple function named square:
function square(number) {
return number ** 2;
}
The function square takes one argument, called number. The function consists of one statement that says to return the argument of the function (that is, number) multiplied by itself. The return statement specifies the value returned by the function.
return number * number;
Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.