Introduction to Javascript
Javascript is a scripting or dynamic computer programming language. It works asynchronously. Javascript can change an updated HTML and CSS. Nowadays javascript can be used client-side and server-side also.
console.log("Hello Programmer!");// Output is:
Hello Programmer
Javascript Primitives Value: javascript has 7 primitive data types number, string, boolean, symbol, bigint, Undefined and Null. All primitives are constant, they cannot be altered.
console.log(8);
console.log('hello bangladesh');
console.log(null);
console.log(undefine);
OOP in Javascript: OPP stands by object-oriented programming. OOP is faster and easier to execute. It provides clean code.
class Person{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
getFullName() {
return this.firstName + " " + this.lastName;
}
}
const person1 = new Person('Iftekhar' , 'Hossain');
console.log(person1)
What is ES6: In June 2015 javascript release the 6th edition ECMAScript 6 (or ES6) and later renamed ECMAScript2015. Javascript ES6 brings so many new features and syntax.
Block Bindings: In JavaScript, variable declare is an important factor. In a variable declaration, it depends on which methods you will declare a variable and where it will be. ES6 provides a new way to declare so that you can more easily control the scope of variables.
Block-Level Declarations: Block-level declarations mean the declared variable is not accessible outside the block scope Block scopes are created:
- Inside of a function
- Inside of a block (indicated by the
{
and}
characters)
Var Declarations and Hoisting: Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting. Example-
function getName(condition) { if (condition) { var name; // other code
name= "Emon";
return name;
} else {
// name doesn’t exist here
return null;
}
// name doesn’t exist here
}
in the example, the variable is declared to the inside of the if condition. so that the name variable is not accessible on the else condition and outside of the condition.
function getName(condition) { var name; if (condition) {
name= "Emon"; // other code return name;
} else { return null;
}
}
The declaration of name variable is hoisted to the top. That means the variable name is accessible from the else condition. If accessed from there, the variable would just have a value of undefine because it hasn’t been initialized.
Let Declarations: The let and var declaration syntax is the same. You can use let in place of declaring var. And if you do this you have to remember that the scope of the variable will remain in the current block because var is hoisted on the top of the function but let is not hoisted.
let name = "iftekhar emon";
console.log(name);
Const Declarations: If we declared a variable with the const variable, we can’t change this value. The value is constant.
const age = 23;
console.log(age);
Block Binding in Loops: In JavaScript, most often case developers want block-level scoping of variables is within for loops. For for this we have to use let not var, cause var is being hoisted. Follow the two examples below:
for (var i=0; i < 10; i++) {
process(items[i]);
}// i is still accessible here
console.log(i); // 10
Here, outside the loop, i is accessible because we use var
for (let i=0; i < 10; i++) {
process(items[i]);
}// i is not accessible here - throws an error
console.log(i);