<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Elina425</title>
    <description>The latest articles on DEV Community by Elina425 (@elina_melkonyan).</description>
    <link>https://dev.to/elina_melkonyan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F924860%2Fb3c30642-bed7-4a32-9ea1-f64f092d143d.png</url>
      <title>DEV Community: Elina425</title>
      <link>https://dev.to/elina_melkonyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elina_melkonyan"/>
    <language>en</language>
    <item>
      <title>Functions in Javascript</title>
      <dc:creator>Elina425</dc:creator>
      <pubDate>Wed, 09 Nov 2022 20:59:10 +0000</pubDate>
      <link>https://dev.to/elina_melkonyan/functions-in-javascript-348c</link>
      <guid>https://dev.to/elina_melkonyan/functions-in-javascript-348c</guid>
      <description>&lt;h2&gt;
  
  
  What is a Function?
&lt;/h2&gt;

&lt;p&gt;Function, generally speaking, is a reusable piece of code that is designed to perform a particular task.&lt;/p&gt;

&lt;p&gt;It is composed of a series of statements called function body. Values can be passed to a function and the function can/will return a value.&lt;/p&gt;

&lt;p&gt;To create a function we can use a function declaration.&lt;/p&gt;

&lt;p&gt;It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function showRandomStaff() {
  alert( 'Hello everyone!' );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function keyword goes first, then goes the name of the function, then a list of parameters between the parentheses (comma-separated, empty in the example above, we’ll see examples later) and finally the code of the function, also named “the function body”, between curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function name(parameter1, parameter2, ... parameterN) {
 // body
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if I do like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function showRandomStaff() {
  alert( 'Hello everyone!' );
}

showRandomStaff()
showRandomStaff()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The call showRandomStaff() executes the code of the function. Here we will see the message two times.&lt;/p&gt;

&lt;p&gt;This example clearly demonstrates one of the main purposes of functions: to avoid code duplication.&lt;/p&gt;

&lt;p&gt;If we ever need to change the message or the way it is shown, it’s enough to modify the code in one place: the function which outputs it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function that does not take a parameter and doesn’t return anything.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sayHello () {
  console.log("Hello World!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function does not take a parameter and doesn’t return a value;&lt;/p&gt;

&lt;p&gt;What do I mean when I say, the above function doesn’t return anything?.&lt;/p&gt;

&lt;p&gt;If I try to store the result of calling the function in a variable, it will be undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let greeting = sayHello();
// The below console.log will return undefined as the function 
// doesn't return any value.
console.log (greeting);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A function that takes a parameter and returns a value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s write a function that takes a number as a parameter and returns the square of the number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function square(number) {
 return number * number;
}
console.log(square(3));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above function execution is 9.&lt;/p&gt;

&lt;p&gt;When a value is passed as a function parameter, it’s also called an argument.&lt;/p&gt;

&lt;p&gt;In other words, to put these terms straight:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A parameter is the variable listed inside the parentheses in the function declaration (it’s a declaration time term).&lt;/li&gt;
&lt;li&gt;An argument is the value that is passed to the function when it is called (it’s a call time term).
We declare functions listing their parameters, then call them passing arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example above, one might say: "the function square() is declared with 1 parameter, then called with 1 argument: number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default values&lt;/strong&gt;&lt;br&gt;
If a function is called, but an argument is not provided, then the corresponding value becomes undefined.&lt;/p&gt;

&lt;p&gt;For instance, the aforementioned function square(number) without argument will print undefined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Differences between Parameters and Arguments&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(x, y){
    return x + y
}

add(2, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code x and y are parameters while 2 and 3 are the arguments here.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;parameter&lt;/em&gt; is one of the variables in a function. And when a method is called, the &lt;em&gt;arguments&lt;/em&gt; are the data you pass into the method's parameters.&lt;/p&gt;

&lt;p&gt;When the function is called with add(2, 3) the arguments 2 and 3 are assigned to x and y, respectively. This means that in the function, x will be replaced with 2 and y will be replaced with 3.&lt;/p&gt;

&lt;p&gt;If the function is called with a different argument, the add function will work the same way. Parameters are like placeholders for function arguments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Power of Arguments&lt;/strong&gt;&lt;br&gt;
We can use arguments more efficiently when we want to make functions more re-useable, or when we want to make calling functions inside another functions more powerful.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(x, y){
    return x + y
}
function substruct(n1,n2){
        return n1 - n2;
}
function multiply(a, b, c){ // a = 1, b = 2, c = 3
    const num1 = add(a, b) // num1 = add(1, 2) = 3
    const num2 = substruct(b, c) // num2 = substruct(2, 3) = -1

    return num1 * num2 // -3
}

multiply(1, 2, 3)
// returns -3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first function add() has two parameters, x and y and second function substruct() has two parameters, n1 and n2 . The functions returns the addition and substruction of the two parameters.&lt;/p&gt;

&lt;p&gt;The second function multiply() has three parameters: inside the function, two variables are declared in it, num1 and num2. num1 will store the value of the result of add(a, b), and num2 will store the value of the result of substruct(b, c). At the end the multiply function will return the value of num1 multiplied by num2.&lt;/p&gt;

&lt;p&gt;multiply is called with three arguments which are 1, 2 and 3. add(a, b) will be add(1, 2) which will return 3. substruct(b, c) will be substruct(2, 3) which will return -1.&lt;/p&gt;

&lt;p&gt;num1 will have the value of 3 while num2 will be -1. num1 * num2 will return -3.&lt;/p&gt;

&lt;p&gt;Arguments are passed in the multiply function which are also used as arguments for the add function and substruct function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions are treated as First-Class in Javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Functions are treated as first-class in Javascript because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can assign a function to variable&lt;/li&gt;
&lt;li&gt;We can store function in array&lt;/li&gt;
&lt;li&gt;A function can be passed to another function as argument&lt;/li&gt;
&lt;li&gt;A function can be assigned to a property of object
But let’s see how to assign the above function into a variable and use it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const square = function (number) {
  return number * number;
}
console.log(square(3));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The directive &lt;em&gt;return&lt;/em&gt; can be in any place of the function. When the execution reaches it, the function stops, and the value is returned to the calling code (assigned square above).&lt;/p&gt;

&lt;p&gt;There may be many occurrences of return in a single function. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkAge(age) {
  if (age &amp;gt;= 18) {
    return true;
  } else {
    return confirm('Your age is illegal for such activities!!!');
  }
}

let age = prompt('How old are you?', 18);

if ( checkAge(age) ) {
  alert( 'Access granted' );
} else {
  alert( 'Access denied' );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets see how can we store function in array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(num1, num3){
   return num1 + num2
}

let myArray = [10,30,sum]
let result = myArray[2](myArray[0],myArray[1])
console.log(result)//40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;10 and 30 are integers and sum is a function. So, I'm passing the  first and second elements of myArray to myArray[2] which is basically my function sum(). You should expect this function to print the sum of 10 and 30(which is 40).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passing function to another function as an argument&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function inner(name){
    console.log("Hello " + name)
}

function returnFunc(name){
 return name;
}

function outer(myFunc, name){
    myFunc(name)
    return returnFunc(name)
}

let returnVal = outer(inner, "Bob")
console.log(returnVal)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case function inner() will become the first argument of function outer().&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Assigning function to a property of object *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let employee = {
name:"James",
printMessage:function (){
    console.log("Welcome dear " + this.name)
}
employee.printMessage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are assigning function to printMessage, so when we are writing employee.printMessage, it's basically calling the function and printing the message that we have written there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understand Function Scope and Block Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Function scope&lt;/em&gt;&lt;br&gt;
This scope means that the variables are only accessible in the function in which they are declared.&lt;/p&gt;

&lt;p&gt;Consider the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fun()
{
    var temp ="temp is defined in function scope";
    console.log(temp); 
}

fun();
console.log(temp);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run the above code, we can see that the console.log(temp) statement outside of function throws an error because the temp variable is not accessible outside the function it is declared in.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Block scope&lt;/em&gt;&lt;br&gt;
The block scope of a variable means that the variable is accessible within the block that is between the curly braces.&lt;/p&gt;

&lt;p&gt;Consider the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(true)
{
  let v1 =10;
  let v2=20;
  console.log(v1);
  console.log(v2); 
}

console.log(v1);
console.log(v2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we defined some variables in the if block and printed them to the console.&lt;br&gt;
When you run the above code, you can see that an error occurs while printing the variables v1 and v2. As let variables are block-scope based, v1 and v2 will not be accessible outside the if block.&lt;/p&gt;

&lt;p&gt;Remember if you wanna your programming skills work or function nicely you should know how to use functions as there are gonna be at least 80% of your every code!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Variables in Javascript</title>
      <dc:creator>Elina425</dc:creator>
      <pubDate>Sun, 06 Nov 2022 18:39:54 +0000</pubDate>
      <link>https://dev.to/elina_melkonyan/variables-in-javascript-242a</link>
      <guid>https://dev.to/elina_melkonyan/variables-in-javascript-242a</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Variable ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you wanna become a programmer, then variables will become inseparable part of your code and maybe your best friend. But what are variables? You can imagine them as a box or a bottle in which we can store different types of data, we can add different numbers, text, etc. In order to be more specific, variables act like a container for storing String, Number, Boolean, and complex data structure(Arrays, Objects) and even the entire function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JS is a dynamically typed language, so we generally don’t have to worry about assigning the type of data that is being stored in that variable. However, knowing the types makes debugging a lot easier. So, JS is smart enough to figure out what you have assigned to it and when you make changes it adapts accordingly(cool!!!). In most of the famous and older languages like java, c++ you have to specify what data type(Numbers, Strings, Float, Double, etc) a variable will contain. &lt;br&gt;
JavaScript defines seven built-in types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String ===&amp;gt; var name = “Jessy”&lt;/li&gt;
&lt;li&gt;Number ===&amp;gt; var age = 18&lt;/li&gt;
&lt;li&gt;Boolean ===&amp;gt; var isActive = true&lt;/li&gt;
&lt;li&gt;Array ===&amp;gt; var colors = [“blue”,”red”,"orange"]&lt;/li&gt;
&lt;li&gt;Objects ===&amp;gt; var person = { name: “jenny”, age: 25, gender:"female" }&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Note: All of these types except object are called “primitives”. *&lt;/em&gt; Actually, you'll need to remember more primitive types in case of switching to another programming language(DON'T YOU DARE!).&lt;br&gt;
Also, remember that in JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. &lt;/p&gt;

&lt;p&gt;Now let’s see how we define a variable in Javascript.&lt;/p&gt;

&lt;p&gt;There are 3 ways of declaring a Variable:&lt;/p&gt;

&lt;p&gt;Using Var Keyword&lt;br&gt;
Using Let Keyword&lt;br&gt;
Using Const Keyword&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declaring a Variable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age;
let name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To declare a variable use let or var keyword before your variable name. When we declare a variable, it is stored in the memory by Javascript engine with the default value of undefined ( if we don’t assign any value at the time of declaration). In this case, as we are only declaring them, at this moment they don’t contain any value and if you console their values, it will return undefined.&lt;/p&gt;

&lt;p&gt;You can also declare multiple variables at the same time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age, name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Assigning a value to the Variable**&lt;br&gt;
After declaring the variable, you can assign a value to it using the = operator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age, name;
age = 18;
name = "Jenny";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the time it's more comfortable to declare and assign a value at the same time&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 18;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of a above variable age can be accessed by simply calling it by its name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(age) //18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But make sure that the variable you're calling exist, because if not it'll throw an error.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(ankap) // Uncaught ReferenceError: ankap is not defined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Naming Variables in Javascript:&lt;/strong&gt;&lt;br&gt;
Naming variables properly is very essential as firstly you can't name variable however you want. It's better when the name of your variable is simple and describes the object, array, string, etc or in short what information it stores. When you declare variables, there’s a bunch of different options. The style is up to you, but just to keep it simple you can start from Camel Casing.&lt;br&gt;
&lt;code&gt;boolean isEven = true&lt;/code&gt; &lt;br&gt;
Besides, there exist certain rules, which you can't neglect, but rather have to learn:&lt;br&gt;
1) Don’t use reserved keywords of Javascript (like class, var, etc).&lt;/p&gt;

&lt;p&gt;2) Variables are case sensitive, isActive variable is different from isactive&lt;/p&gt;

&lt;p&gt;3) A variable name cannot begin with a number, underscore, symbols.&lt;/p&gt;

&lt;p&gt;4) Variable Name can contain a mix of uppercase strings, lowercase strings, and numbers.&lt;/p&gt;

&lt;p&gt;5) A variable name should always start with lowercase.&lt;/p&gt;
&lt;h2&gt;
  
  
  var vs const vs let
&lt;/h2&gt;

&lt;p&gt;As previously was said you can declare variable in 3 different ways:var, let, const&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Oldest way of defining a variable in JS. It was king of the block for a long time, until it’s much more specific brothers came long.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;can be changed&lt;/li&gt;
&lt;li&gt;scoped globally or locally to an entire function regardless of block scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0WGW1_TO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3c5p9a4hctqpzywkyf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0WGW1_TO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3c5p9a4hctqpzywkyf9.png" alt="Image description" width="880" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Var variables can be accessed before their declaration. Javascript moves all the var variables to the top of the function or global context. This is known as var hoisting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REDECLARATION OF VAR VARIABLES:&lt;/strong&gt;&lt;br&gt;
var variables can be redeclared in the same scope. It will override the existing variable. Avoid using var as you might accidentally redeclare the same variable again and that will make your program behave differently as it will not show any errors if the variable name is already used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num1 = 8;
if(num1 % 2 == 0){
   var num1 = 9;//no error
   console.log(num1);//9
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we redeclare the num1 variable in the if block and it will override the existing num1 variable. This is because var variables have function scope and are available throughout the function as we discussed earlier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;can be changed.&lt;br&gt;
scoped to the block.&lt;br&gt;
let variables are not initialized until their definition is evaluated. If you try to access it before declaration then you will get an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(num); //Uncaught ReferenceError: num is not defined
let num;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;REDECLARATION OF LET VARIABLE:&lt;br&gt;
Variables declared using let keyword cannot be redeclared within the same scope, it will give an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printNums(){
   let number = 60;
    if(number &amp;gt; 0){
      let number =40;//no error because it has different scope
      console.log(number);//40
}

let number = 90;//Uncaught SyntaxError: Identifier 'number' has already been declared
console.log(number);//90
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;let variables cannot be redeclared in the same scope&lt;/em&gt;&lt;br&gt;
The number variable inside the if block will not give any error because let variable creates a new scope inside any pair of curly braces. The number variable outside the if block gives an error because of the same scope.&lt;br&gt;
&lt;strong&gt;const&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;cannot be changed after instantiation.&lt;br&gt;
scoped to the block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pi = 3.14;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare those variables as a constant which you don't want to change in the entire program&lt;/p&gt;

&lt;p&gt;Remember that constant variable must be initialized with some value otherwise it will give a syntax error.&lt;br&gt;
&lt;code&gt;const job; //Uncaught SyntaxError: Missing initializer in const declaration&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But, this is not in the case of Objects. We can change the properties of an Object although we declare it as constant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
  name:"Tom",
  age:13
}
person.name =  "Bob"// change the existing name property
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But you cannot change the entire object that is declared constant. If you do, it will give an error.&lt;br&gt;
&lt;code&gt;person = {gender: male}//Uncaught TypeError:Assignment to constant variable&lt;/code&gt;&lt;br&gt;
REDECLARATION OF CONSTANT:&lt;br&gt;
Constants cannot change through re-assignment and cannot be re-declared in the same scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O9MOAhpu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7veb7mj3ill1nuiusdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O9MOAhpu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7veb7mj3ill1nuiusdw.png" alt="Image description" width="814" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;when to use let vs const&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two camps about how to approach when to use what.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I don’t trust anyone&lt;/em&gt;&lt;br&gt;
This method says, use const first for every variable. If the need comes along that you need to change a variable after it's been declared, change it to let.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I trust myself&lt;/em&gt;&lt;br&gt;
This method says, use let for everything. If the need comes along that you need to make sure a variable can't be changed, change it to const.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do’s and Dont’s&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SdU0Vv_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9rfd63rqwf0musjpgy5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SdU0Vv_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9rfd63rqwf0musjpgy5.png" alt="Image description" width="880" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefer let over var to define variables as let will keep our variables in the right scope and make our code more manageable.&lt;/li&gt;
&lt;li&gt;Use const to define those values which you don’t want to change in your entire application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use let and var to define variables and const to define constants.&lt;/li&gt;
&lt;li&gt;let and const has block scope whereas var has function scope.&lt;/li&gt;
&lt;li&gt;var variables are added to the global object and can be accessed using the window object but let variables are only limited to their block and cannot be accessed via a window object.&lt;/li&gt;
&lt;li&gt;Unlike var, let variables are not hoisted&lt;/li&gt;
&lt;li&gt;The properties of Constant Object can be changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope, I've mentioned all the essential parts regarding variables, if not feel free to extend your knowledge more!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Binary numbers</title>
      <dc:creator>Elina425</dc:creator>
      <pubDate>Thu, 15 Sep 2022 18:57:10 +0000</pubDate>
      <link>https://dev.to/elina_melkonyan/binary-numbers-3im4</link>
      <guid>https://dev.to/elina_melkonyan/binary-numbers-3im4</guid>
      <description>&lt;p&gt;Have you ever wandered what are binary numbers? I'm quit sure that probably not once you have seen those cool pictures full of 1s and 0s, which are somehow connected with computers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lYzmc914--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kb7302pewus302i8c84.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lYzmc914--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kb7302pewus302i8c84.jpg" alt="Image description" width="509" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just as we have the decimal system for counting numbers in everyday life, the same way computers use binary system for calculations. So, it's just another system of counting. With binary numbers we can represent numbers from 0 up to 255(yehhh..., they are better than base 10 system). In case of a decimal system computer would have to work harder and longer in order to process things. Fortunately, binary occupies less space and is much more simpler. We can think of binary as a light switch(When the light is on we have 1 and when off 0). Sounds pretty easy, but wait!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PP0DaBlH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwwdquf2hxjkoftoo5ah.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PP0DaBlH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwwdquf2hxjkoftoo5ah.jpg" alt="Image description" width="642" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You need to know how to convert decimal numbers to binary as well the reverse process. Of course you can use automatic converters but it's better to know how to do it ourselves. Here are the steps for decimal to binary convertion:&lt;br&gt;
Step 1: Divide the given decimal number by “2” where it gives the result along with the remainder.&lt;/p&gt;

&lt;p&gt;Step 2: If the given decimal number is even, then the result will be whole and it gives the remainder “0”&lt;/p&gt;

&lt;p&gt;Step 3: If the given decimal number is odd, then the result is not divided properly and it gives the remainder “1”.&lt;/p&gt;

&lt;p&gt;Step 4: By placing all the remainders in order in such a way, the Least Significant Bit (LSB) at the top and Most Significant Bit (MSB) at the bottom, the required binary number will be obtained.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qXptFXcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mm1xz0ja1ra22zx5swm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qXptFXcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mm1xz0ja1ra22zx5swm.jpg" alt="Image description" width="880" height="804"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, watch this video:&lt;a href="https://youtu.be/rsxT4FfRBaM"&gt;https://youtu.be/rsxT4FfRBaM&lt;/a&gt;&lt;br&gt;
The process of converting binary to decimal is even simpler, so just watch the video: &lt;a href="https://youtu.be/VLflTjd3lWA"&gt;https://youtu.be/VLflTjd3lWA&lt;/a&gt; and you will understand that for example 11100=2^4+2^3+2^2=28&lt;/p&gt;

&lt;p&gt;Ok, let's now pass to more complicated topics like binary operations. Basically, it's very similar to decimal operations. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fmGO4OmR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dmd0kfybhqm79nbuduf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fmGO4OmR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dmd0kfybhqm79nbuduf.png" alt="Image description" width="324" height="183"&gt;&lt;/a&gt;&lt;br&gt;
Having the above table in mind you can do addition confidently like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RPav_ysb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wu8qqhvg5w80hgmlyb4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RPav_ysb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wu8qqhvg5w80hgmlyb4b.png" alt="Image description" width="210" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: Remember that 1+1+1=11 or we can say that the sum is 1 and we have 1 carry.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3WJC-zdw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sebafqmx0qrvx747zpwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3WJC-zdw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sebafqmx0qrvx747zpwl.png" alt="Image description" width="240" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's learn a more complicated operations like multiplication. It's very much similar to decimal multiplication. The only difference is that we use 1s and 0s. For multiplication you should remember these 'rules':&lt;br&gt;
There are four rules of binary multiplication which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 × 0 = 0&lt;/li&gt;
&lt;li&gt;0 × 1 = 0&lt;/li&gt;
&lt;li&gt;1 × 0 = 0&lt;/li&gt;
&lt;li&gt;1 × 1 = 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After knowing these super simple rules you only need to multiply each digit of one binary number to each digit of another binary number. And then add them all together to get the final result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cptgr2gQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/744s132tb69qfpckv4zz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cptgr2gQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/744s132tb69qfpckv4zz.png" alt="Image description" width="294" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, I don't wanna disappoint you but it's also essential to know binary subtraction, but don't worry as after doing a couple of exercises it'll surely become easy. Generally, we apply these rules for subtraction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 - 0 = 0&lt;/li&gt;
&lt;li&gt;1 - 0 = 1&lt;/li&gt;
&lt;li&gt;1 - 1 = 0&lt;/li&gt;
&lt;li&gt;0 - 1 = 1 with a borrow of 1 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Actually, when you borrow 1 the O becomes 10 or 2 and 2-1=1 that's why 0 - 1 = 1&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RVLiBEoW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkgw7ksn5ph1bolboruv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RVLiBEoW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkgw7ksn5ph1bolboruv.png" alt="Image description" width="636" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Knowing this much it pretty much enough!!! You can also have some fundamental understanding of binary division, but there is no need to dive too much into it. The main rules of the binary division include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1÷1 = 1&lt;/li&gt;
&lt;li&gt;1÷0 = Meaningless&lt;/li&gt;
&lt;li&gt;0÷1 = 0&lt;/li&gt;
&lt;li&gt;0÷0 = Meaningless&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just to have a practice let's divide 18 by 3 which gives 6 :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h3XMFwOn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhgjld5b52a9odhegibt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h3XMFwOn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhgjld5b52a9odhegibt.png" alt="Image description" width="695" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it!!!Now you know how to do basic operations with binary numbers, hence you understand ''computer language'' better. Hope you enjoyed reading this post!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>binary</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
