Basic Knowledge About Javascript
--
Hello, my programmer's friends!!
Today I will describe to you some basic knowledge about javascript. Are you ready …!!
Let's start now!
Types of values in Javascript: Friends!!
Are you see the sky!! If you see the sky then you see many stars in the sky.!! Sure!?
So my friends…. Imagine Javascript is like the sky. In this sky many stars which are called“values”. Are you confused !! No No … I say details ….
I hope that you see many values in Javascript. But you should know here many types in this values. If you console these then you will understand something…
console.log(2);
console.log(“Hi”);
console.log(undefined);
console.log({});
console.log([]);
console.log(x => x * 2);
Details in types:
The type of values is 9:
Undefined: This type of value you show when you use unintentionally missing values.
const friends = { name: “sakib”, age: 20 };console.log(friends.baby);
//output: undefind
Null: This type of value you show when you intentionally missing values.
Booleans: This type when you store your value or see by true or false, and used for logical operations.
Numbers: This type when your value is number.
Strings: This type you see when your value is a string.
Symbols, BigInts These two types are uncommon.
Hello programmers… attention, please!!
These 7 types called Premative values.And two types without these which is object and function type. Like this..
console.log({});
console.log([]);
console.log(y => y* 5);
Find types: You will ask me now… How you find the type of values!? SO my programmer's friend!! you use this method “typeOf()”. Like this…
console.log(typeof(8)); // “number”
console.log(typeof(“Ahsan Ullah”)); // “string”
Error Handling: I know you are a programmer. It doesn't matter you are a junior or senior programmer!! But When you start programming you face always many problems and errors. SO now I am discussing that “how to solve your problem very easily and efficiently way !?”
My programmer's friends when you face a problem or error then our ‘Javascript ’ Boss sends you an error message. Many times you don't understand this. So if you use the “try….catch” method for error handling then you understand your error very easily. You should know that this error message an object. Here three parts … 1.error name 2.error message 3.error stack. If you use the “try…catch” method then you can differentiate between these.
Now you want to know what is “ try…catch” method!? So look at this code…
const friends = [{ name: “sakib”, age: 20 }];try {console.log(friendss);} catch (error) {console.log(error.message);}
//output error message: friendss is not defined.
In the “try…catch” method two blocks. Under the “try{….}” blocks you write your code and under the “catch(error){……} ” blocks you write your error handling code. If you write “error.name” then you get your error name.
But my friends… this is works in run time errors. I mean if your code won't have any syntactical error then this method will work.
Set new error message: You are a programmer. So you want to set a new error message for your code. You do this like this code …
const friends = [{ name: “sakib”, age: 20 }];try {console.log(friends.baby);if (!friends.baby) {throw new SyntaxError(“This is my error”);}} catch (error) {console.log(error.name, error.message);}
//output: SyntaxError This is my error
Here I use the “throw” operator which generates a SyntaxError by my message.
Block Bindings: Are you know my friends!! what are block bindings!?
So you should know that when we declare our variable by using var, let and const then occur Binding. Like this…
var number = 1;
let name = "Ahsan;
const friend = "sakib";
Var Declarations and Hoisting: If we are declaring our variable by using var then declare this on the top in function or global scope if declared outside the function.
function getBabyName(condition) {
if (condition) {
var babyName = 'babu'
return babyName
} else {
return 'baby is not found'
}
}
But here a problem you face some time. That if you declare your function by var on the top then you access this variable from outside. Like this…
function getBabyName(condition) {var babyName;
if (condition) {
babyName ="babu"
return babyName
} else {
//babyName accessable from here
return 'baby is not found'
} //babyName accessable from here
}
Block-Level Declarations: We show that in our variable when use var that accessible from outside the block scope. But if we declare our variable by let and cost then it's doesn't accessible from the outside block. It's fixed in a block.
function getBabyName(condition) {function getBabyName(condition) {
if (condition) {
let babyName = 'babu'
return babyName
} else {
//don't accessible babyName from here
return 'baby is not found'
}//don't accessible babyName from here
}
Spread Operator: This is a special feature of ES6. Which do the work by javascript very easier. It's useful for many different tasks like …
- Copying an existing array
- Merge or combine arrays
- Call function without apply
I am saying to you one example...
let numbers= [6, 10, 21]
console.log(...numbers)
//output: 6 10 21
Arrow Functions: Hello my friends!! ES6 made a very easier function declaration. Which called is the “arrow function”…
// lets create function that add two numbers with our old system:
function add(x, y) {
return x+ y
}
console.log(add(21, 10)) //31
// same function using arrow function , where x and y are function parameter:
let add2 = (x, y) => {
return x+y;
}
console.log(add2(21, 10)) // 31
So my dear programmer friends today I close my story. I am coming soon in the next story….