Posts

Create a String in js

Image
  String    there are three ways to create a string in javascript  : 1- Literal creation . ✅          let myStr = " Hend Ahmed";            console.log(typeOf(myStr)); // string   2- String Function . ✅          let myStr = string("Hend Ahmed");           console.log(typeOf(myStr)); // string    3- Constructor .  (❌ Dont use this method)          let myStr = new string("Hend Ahmed");           console.log(typeOf(myStr)); // Object  so Don't use the last way because it causes a complicated code and a slow performance .

Data Type Coercion in js

Image
    What is Type Coercion in JavaScript ? Type Coercion refers to the process of automatic or implicit conversion of values from one data type to another. This includes conversion from Number to String, String to Number, Boolean to Number etc. when different types of operators are applied to the values. In case the behavior of the implicit conversion is not sure, the constructors of a data type can be used to convert any value to that datatype, like the Number() , String() or Boolean() constructor. 1. String to Number Conversion: When any string or non-string value is added to a string, it always converts the non-string value to a string implicitly. When the string ‘Rahul’ is added to the number 10 then JavaScript does not give an error. It converts the number 10 to string ’10’ using coercion and then concatenates both the strings. Some more examples are shown below.    

Hoisting

Image
            Hoisting   JavaScript hoists function declarations and variable declarations to the top of the current scope. Variable assignments are not hoisted ( JavaScript only hoists declarations, not initializations!). Declare functions and variables at the top of your scripts, so the syntax and behavior are consistent with each other.        

Shadowing

Image
  What is shadowing in javascript ?   Shadowing: Now, when a variable is declared in a certain scope having the same name defined on its outer scope and when we call the variable from the inner scope, the value assigned to the variable in the inner scope is the value that will be stored in the variable in the memory space. This is known as Shadowing or Variable Shadowing .       What is illegal shadowing ? Illegal Shadowing: Now, while shadowing a variable, it should not cross the boundary of the scope, i.e. we can shadow var variable by let variable but cannot do the opposite. So, if we try to shadow let variable by var variable, it is known as Illegal Shadowing and it will give the error as “variable is already defined.”

How the variable inside the function being Global !

Image
"z will be Global after the first call of the function "  

Variable Scope in javascript

Image
      Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: Block scope Function scope Global scope   1. Block Scope : Before ES6 (2015), JavaScript had only Global Scope and Function Scope . ES6 introduced two important new JavaScript keywords: let and const . These two keywords provide Block Scope in JavaScript.  --------------------------------------------------------------------------------------------- Variables declared inside a { } block cannot be accessed from outside the block: Example: {    let x = 2 ; } // x can NOT be used here    ---------------------------------------------------------------------------------------------- Variables declared with the var keyword can NOT have block scope. Variables declared inside a { } block can be accessed from outside the block.  Example: {    var x = 2 ; } // x CAN be used here       note...

isFinite( )

Image
        isFinite( ); this built in function is used to check if the data Numerical value or String value . isFinite( ) :  Check if a number is finite . isFinite( ) : return true or false depending on the argument.    The isFinite() method returns true if a value is a finite number. Infinite (not finite) values are Infinity , -Infinity , or NaN   for example : isFinite(170538);       \\ return true isFinite("189anbz46");    \\ return false