
JavaScript is a programming language which helps you to create a responsive web pages.
JavaScript is also a client-side as well as server-side scripting language.
JavaScript is mainly used for to make a webpages interactive.
JavaScript allows users to interact with web pages.
Variables
Variables are containers for storing the data values.
There are 3 ways to declare a JavaScript variables
1. Using var
2. Using let and
3. Using const
1. Using var
For example num1,num2 and num3 are the variable declared with the var keyword.
var num1 = 10;
var num2 = 20;
var sum = num1 + num2;
2. Using let
Now we can declare a variables num1, num2 and num3 using let keyword.
For example
let num1 = 10;
let num2 = 20;
let num3 = num1 + num2;
3. Using const
Constants are the block scoped and the constant values can't be changed through assignment and cannot be redecleard.
For example
const size1 = 10;
const size2 = 20;
Let totalSize = size1 + size2;
In the above example size1 and size2 are the variables and declared using the const keyword.
These values can't be changed. The totalSize is declared using let keyword the value can be changed here.
a = 10;
b = 20;
c = a + b;
In the above example a, b and c are the undeclared variables.
And here you can guess a variable is storing the value 10 and b variable is storing the value 20.
Always declare a javascript variables using var, let and const keyword.
JavaScript Identifiers
All JavaScript variables are must identified with their unique names.
These unique names are called identifiers.
Identifiers can be any names for example x, name, sum, totalSum etc.
For creating a variables there are some rules followed by
1. Names must be starts with letters or alphabets.
2. Names cannot starts with special characters except $(dollar) and _(underscore).
3. It can have a numbers but cannot starts with the numbers.
4. Names are case sensitive (name and NAME are the different).
5. Reserved keywords cannot be used as a names i.e. JavaScript keywords.
JavaScript DtaTypes
JavaScript variables can hold different datatypes like integer, string, Boolean, float etc.
For example let x = 25;
In the above example x variable holding the number 25 i.e. integer.
Let name = “Priyanka”;
In the above example variable name is holding the value string.
Let me take one example
let sum = “25” + “Priyanka”;
The output will be 25Priyanka because if we written any integers in double quotes or single quotes it will consider as a string.
Let me take example1
let sum = 10 + 20 + “Priyanka”;
In the above example output will be 30Priyanka because 10 and 20 is considered as a integers until it will reaches the “Priyanka”.
Let me take example2
let sum = “Priyanka” + 10 + 20;
In the above example first operand is string, so all operands are treated as a string.
Boolean can have only two values true and false.
Let me take one example
let num1 = 10;
let num2 = 20;
let num3 = 10;
(num1 == num2)
(num1 == num3)
In the above example (num1 == num2) will give the output is “False “, and (num1 == num3) output will be “True”.
JavaScript Objects
In JavaScript objects are also variables too.
In JavaScript objects are unordered collection of key-value pairs. Each key value pair is called as property. The key of a property can be string and the value of a property can be any value like a string, an array, a numbers, and even a function.
To create an object with properties you use the key:value pair within the curley braces.
For example following creates a employee objects with some properties.
let employee = { myName : “Priyanka”,
Location : “ Bangalore” };
In the above example employee object created with myName and Location properties with the values Priyanka and Bangalore. When the object has multiple properties separate them using comma ( , ) like above example.
For access a property of an object use two notations dot notation( . ) and array-like( [] ) notation.
1. Dot notation :
Syntax : objectName.propertyName
In the above example to acces the myName property in employee object you can use the following expression
employee.myName
In the below example creates a Employee object and it will print the myName and Location in the console.
let employee = {myName : “Priyanka”,
Location : “Bangalore”};
Console.log(employee.myName);
Console.log(employee.Location);
2. Array-like notation ( [] ) :
Syntax : objectName[“propertyName”]
For example
let Person = {fullName : “Pooja”, age : 26}; console.log(Person[“age”]);
JavaScript Functions :
JavaScript Function is used to perform a particular task. when developing an applications we aften need to perform a perticular task in many times. for example you may want to show some message whenever an error occurs. To avoid the repeating the same code in all the time, you can use a function to call that code and reuse it.
How to Declare a Function?
To declare a function you can use the function keyword, followed by the function name, a list of parameters inside the parenthesis().
function functionName(parameters){ //function body }
Some rules to declare a function as follows
1. Function names can have a numbers but should not starts with the numbers.
2. Functions names cannot starts with the special characters except $ and _.
3. Function names must starts with the alphabets.
4. Function names cannot be reserved key words.
Using comma we can separate the parameters like (parameter1, parameter2, parameter3.......)
The code to be executed inside the curly brackets {}.
The code will be execute when "something " calls the function.
Function return
When we write the return statement, function will stop the execution.
For example
function myFunction(a, b){
return a + b;
}
console.log(myFunction(2, 3)); // output 5
In the above example myFunction is a function name, a and b are the parameters.
and function returns the sum of two numbers.
The output will be 5.
Main reason to write a function is define the code once and use it many times.
Local Variables
Local Variables are declared within the JavaScript functions.
Local variables are only be accessed from within the function.
For Example
function myFunction(){ let employeeName = "Priyanka"; }
Since local variable are only recognized inside the functions, variables with the same name can be used in the different functions.
Local variables are created when a function starts, and deleted when the function is deleted.
Arrays
An array is also a special variable which can holds more than one values.
For example
const fruits = ["apple", "orange"]
In above example fruits is an array name, apple and orange are the values of fruits array.
why we use array means if we have a number of values then we have to store in single variable it looks like below example
let fruit1 = "apple"; let fruit2 = "orange"; let fruit3 = "banana";
In an array we can hold multiple values and we can access the values by referring their index numbers.
Syntax for creating an array
const array_name = [item1,item2,....];
using const keyword we can declare an arrays it is a common practice.
We can also create an array first and than we can provide the elements.
For example
copnst fruits = [];
fruits[0] = "Apple";
fruits[1] = "Banana";
fruits[2] = "orange";
We can also create an array by using the JavaScript new keyword.
const cars = new Array("volvo", "BMW");
There is no need to use new Array() method because of simplicity, readability and execution speed, use the literal method.
We can also access the array elements by using the index numbers.
For example
const animals = ["ox", "cow", "dog", "cat"];
let animal = animals[1]; //cow
In above example cow is the output, because array indexes starts with the 0, 0 is the first element and 1 is the second element.
How to change an array element following is the example
let animals[1] = "tiger";
In the above statement changes the value of the 1st element in the animals array.