Javascript Fundamentals You need to know.

1.JSThe basic data type of

There are seven basic data types in JS. They are string, number, BigINT, Boolean, undefined, symbol, and NULL.

So, what are primitive data types? Primitive data types are data types that you cannot modify directly. If you want to change the value of the base data type, then you need to reassign the value,

This shows you what you can and can’t do with primitive data types

let x = 10
x = 20; ̶ ̶. X h ̶ ̶ ̶ ̶ ̶ u p s (̶3̶0̶ ̶)Copy the code

2. Object and function values

Objects and functions are not basic data types because we can modify their values directly through code. Suppose you create an array with a list of five friends’ names. A few days later, you have a new friend you want to add to your list? What would you do? Because the data is an object, it can be modified directly. The following code shows how to add the two friend names directly to the previous array.

//first you have 5 friends
let myFriend = ["Titu"."Nitu"."Ritu"."Pappu"];
//now you have two more friend and you want add
myFriend.push("Zarin"."Nasim");
console.log(myFriend)
//console.log result: (6) ["Titu", "Nitu", "Ritu", "Pappu", "Zarin", "Nasim"]
Copy the code

3.JSThe expression of

Expressions are a core part of every programming language. It brings programming languages to life. Expressions generally take two or more values. It then processes these values and returns their processed results as a single value.

The following example shows the use of JS expressions

let unitPrice = 30;
let totalCount = 10;
// now making result using expression
let totalPrice = (unitPrice * totalCount);
console.log(totalPrice);
//result: 300
Copy the code

4. Type Of

One of the more basic and interesting things about javascript is typeof. It is used to check variable value types supplied by the user or written by the developer. It has many practical uses; One of these is input field validation. If the user mistakenly leaves the field blank or provides a different type of value, we can prompt the user for the correct type of value.

For example: the user asks for his/her age in the input field, but the user mistakenly provides his/her name in that field! Here name is a string and age is a number, in which case we can remind the user to give a value of the correct type. As long as the user doesn’t give us the right type of value, we’re constantly reminding the user to give us the right type of value.

Code examples:

let myAge = "Asif"
if (typeof(myAge) ! ='number'){
  alert("you gave the wrong value");//here this line will pop up as alert because type of my age is not nuber
}
else{
  alert("thanks for giving your age");
}
Copy the code

5. Try-Catch(Lifeguard)

It doesn’t matter how you program; Because you can’t guarantee that your code won’t make mistakes. This can happen for a number of reasons, such as bad code, bad user responses, or an inability to connect to a database.

When something goes wrong in your code, try-catch helps catch the error and feed back rather than kill the program.

Three things you should know about try/catch

  1. It is executed only at run time.
  2. It doesn’t solve syntax errors
  3. If it is scheduled, it cannot resolve the code.

Here is an example of using a try-catch

let x = 10;
try{
  for(i = 0;  i < x; i++){
    console.log(i)// this line will print 0-9
  }
    console.log(w) 
  //here w is not defined so this line will give error and this will catch by catch method
  }
  catch(error){
    let msg = error.message;
    alert(msg);// here alert msg will show that w is not defined
  }
Copy the code

6. Cross-browse tests

Note: Compatibility issues with browser versions

It is important for developers to run cross-browse tests for their code base. Because there are some differences between one browser to another and older browsers to newer versions. If a site doesn’t work in most browsers, it’s not very practical.

Do you have to test all existing browsers? No, absolutely not, because there are hundreds of thousands of browsers on the Internet and you can’t test them all. What you can do is you can test several common browsers to test your code.

7. Expansion operator (magic point)

The expansion operator helps to copy an entire array or object and then easily add one or more values. We use it most of the time when we want to add values to objects.

Example:

let myFriend = ["Rahman"."Kuddus"."Sultan"];
let newFriend = ["jarina"."sakhina"."Beauty"];
let allFriends = [...myFriend,...newFriend];
console.log(allFriends);
// this console log will give name of all 6 friends
view rawspread.js hosted with❤ by makingCopy the code

8. Block-level scopevarVariable declaration!

In JavaScript we can use var to flexibly declare variables. It is a leak variable, meaning that it can leak values from block-level elements into the global environment, causing global values to change.

Example:

var globalValue = 3;
{
  // declaring golbal value in scope
  var golbalValue = 10;
}
console.log(golbalValue)
//output: 10; because var leaks scope value to global. 
Copy the code

Block level scope,constVariable declaration!

Because we know that const is a strict variable declaration, we cannot repeat const declarations of the same name. But at the block level, we know that block-level elements don’t give out-of-scope values or conflict with global variables. So we can redeclare a const variable in both global and local scope.

Example:

const myName = "Murad"
{
  const myName = "Your Name"
  console.log(myName);
  //output: Your Name
}
console.log(myName);
//output: Murad
Copy the code

10. Block-level scopeletVariable declaration!

In JavaScript we can use var to flexibly declare variables. It does not have its scope value accessed by global variables. And it is not a strict type, so we can redeclare the same variable again at the block level, and the two LET variables keep their own values.

Example:

let yourMoney = 500;
const moneyInEid = () = > {
  let yourMoney = 1000;
  console.log(yourMoney);
  //output: 1000
}
moneyInEid()
console.log(yourMoney);
//output: 500;
Copy the code

Var, let, const var, let, const

  1. constThe declared variable is a constant, that is, it can only be declared once and its value cannot be modified
  2. varThere is no block-level scope, whileletThere are block-level scopes.

Sample code is as follows

const fns = [];

// Use var to define variable I
for (var i = 0; i < 10; i++) {
 fns.push(() = > {
   console.log(i); })}// 10 10 10 10 10 10
fns.forEach(fn= > fn())
Copy the code
const fns = [];

// use let to define variable I
for (var i = 0; i < 10; i++) {
 fns.push(() = > {
   console.log(i); })}// 0 1 2 3 4 5 6 7 8 9
fns.forEach(fn= > fn())
Copy the code

See the article: let and const commands for more information