JavaScript Overview
Shahriar Hoq Shubho
Recruiting Manager, International Job Placement at Programming Hero | Team Lead | Technical Recruitment | Project Management | Hiring | Lawyer | Global Networking
What is JavaScript?
JavaScript is a dynamic computer programming language. Its syntax is based on Java and C languages. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities.
JavaScript was first known as LiveScript,?but Netscape changed its name to JavaScript, possibly because of the excitement being generated by Java. JavaScript made its first appearance in Netscape 2.0 in 1995 with the name LiveScript. The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.
Who is JavaScript for?
JavaScript was built as a scripting language and now is used for multiple tasks like:
Fundamental’s part of JavaScript:
· String
· Number
· Boolean
· Function
· Array
· Condition
· Object
· Loop
· Symbol (New in ES2015)
· Null
· Undefine
JavaScript String:
JavaScript strings are the most important component of JavaScript, probably used more than any other data type. Though you may get numeric values from web page forms, the values are retrieved as strings, which you then have to convert into numeric values.
Creating String:?There are 3 Kinds of type to write or create string
const string1 = “Hello Programmer”;
const string2 = ‘Hello Programmer’;
const string3 = `Hello Programmer`;
String has various method like {splice(), replace(), toUppercase(), toLawerCase() etc…
JavaScript Number:?JavaScript has only one type of number. Numbers can be written with or without decimals.
e.g. const num = new Number(100);
JavaScript Boolean:?Boolean is a primitive data type in JavaScript. Boolean can have only two values, true or false. It is useful in controlling program flow using conditional statements like if..else, switch, while, do..while.
Example:
const YES = true;
const NO = false;
if(YES){
alert("This code block will be executed");
}
if(NO)
{
alert("This code block will not be executed");
}
JavaScript Array:?The JavaScript?Array?class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
Declaration
There are two syntaxes for creating an empty array:
let arr = new Array();
let arr = [];
Almost all the time, the second syntax is used. We can supply initial elements in the brackets:
let fruits = ["Apple", "Orange", "Plum"];
You can also create, and use, a literal array in a function or method call:
someFunction("param", ["val1","val2"]);
Array Methods
A?queue?is one of the most common uses of an array. In computer science, this means an ordered collection of elements that supports two operations:
Arrays support both operations.
In practice, we need it very often. For example, a queue of messages that need to be shown on-screen.
There’s another use case for arrays — the data structure named stack.
It supports two operations:
So new elements are added or taken always from the “end”.
A stack is usually illustrated as a pack of cards: new cards are added to the top or taken from the top:
For stacks, the latest pushed item is received first, that’s also called LIFO (Last-In-First-Out) principle. For queues, we have FIFO (First-In-First-Out).
领英推荐
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
JavaScript Functions
JavaScript provides functions similar to most of the scripting and programming languages.
In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want
For example :
//define a function in the name of myfunc
function myfunc() {
alert(“Hello World!”); //write code
}
//calling a function
ShowMessage();
JavaScript Condition
JavaScript includes conditional statements to control the program flow, similar to other programming languages.
JavaScript includes the following forms of if-else conditions:
1. if condition
2. if-else condition
3. else if condition
The tree condition is almost the same syntax. If condition use for only one condition, and the if-else condition is used the two condition that means either or neither and the last else if condition is use for handling multiple conditional statements.
For example:
const mySal = 500;
const yourSal = 1000;
if( mySal > yourSal)
{
alert(“My Salary is greater than your salary”);
}
else if(mySal = yourSal)
{
alert(“My Salary is equal to your salary”);
}
else
{
alert(“My Salary is less than or equal to your salary”);
}
In this condition example first is if condition and else if is executed when the if the condition is false and the last else condition is only executed when the all condition is false.
JavaScript Object
In JavaScript object is king, If You know the object you may the best understand in JavaScript Object is a non-primitive data type in JavaScript. It is like any other variable, the only difference is that an object holds multiple values in terms of properties and methods. Properties can hold values of primitive data types and methods are functions.
Creating Objects in JavaScript
There are 2 ways to create objects.
1. By object literal
2. By creating an instance of Object directly (using the new keyword)
1) JavaScript Object by object literal
The syntax of creating an object using object literal is given below:
object={property1:value1,property2:value2…..propertyN:valueN}
As you can see, property and value is separated by : (colon)
2) By creating an instance of Object
The syntax of creating an object directly is given below:
1. var objectname=new Object();
Here,?new keyword?is used to create object.
JavaScript Loops
The?JavaScript loops?are used?to iterate the piece of code?using for, while, do-while, or for-in loops. It makes the code compact. It is mostly used in the array.
There are four types of loops in JavaScript.
1. for loop
2. while loop
3. do-while loop
4. for-in loop
This article we discussed only for loop. Because in for loop is the most useful loop in JavaScript.
1) JavaScript For loop
The?JavaScript for loop?iterates the elements for a fixed number of times. It should be used if a number of iterations is known. The syntax of for loop is given below.
Syntax:
for(initializer; condition; iteration)
{
// Code to be executed
}
Example: for loop
for (var i = 0; i < 5; i++)
{
console.log(i);
}
In the above example, var i = 0 is an initializer statement where we declare a variable?i?with value 0. The second part,?i < 5?is a condition where it checks whether i is less than 5 or not. The third part,?i++?is iteration statement where we use ++ operator to increase the value of i to 1. All these three parts are separated by semicolon.