<?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: yevastepanyan</title>
    <description>The latest articles on DEV Community by yevastepanyan (@yevastepanyan).</description>
    <link>https://dev.to/yevastepanyan</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%2F928408%2F536322d1-a748-4e2c-9005-c11bf828ae07.png</url>
      <title>DEV Community: yevastepanyan</title>
      <link>https://dev.to/yevastepanyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yevastepanyan"/>
    <language>en</language>
    <item>
      <title>Variables in JavaScript</title>
      <dc:creator>yevastepanyan</dc:creator>
      <pubDate>Thu, 10 Nov 2022 18:26:27 +0000</pubDate>
      <link>https://dev.to/yevastepanyan/variables-in-javascript-273b</link>
      <guid>https://dev.to/yevastepanyan/variables-in-javascript-273b</guid>
      <description>&lt;h2&gt;
  
  
  What are Variables?
&lt;/h2&gt;

&lt;p&gt;Variables are containers for storing data (storing data values).&lt;/p&gt;

&lt;p&gt;In this example, x, y, and z, are variables, declared with the &lt;code&gt;var&lt;/code&gt; keyword:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 5;
var y = 6;
var z = x + y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, x, y, and z, are variables, declared with the &lt;code&gt;let&lt;/code&gt; keyword:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5;
let y = 6;
let z = x + y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, x, y, and z, are undeclared variables:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 5;
y = 6;
z = x + y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From all the examples above, you can guess:&lt;/p&gt;

&lt;p&gt;x stores the value 5&lt;br&gt;
y stores the value 6&lt;br&gt;
z stores the value 11&lt;/p&gt;

&lt;p&gt;Always declare JavaScript variables with &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use JavaScript var?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;var&lt;/code&gt; keyword is used in all JavaScript code from 1995 to 2015.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords were added to JavaScript in 2015.&lt;/p&gt;

&lt;p&gt;If you want your code to run in older browsers, you must use &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use JavaScript const and let?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want a general rule: always declare variables with &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you think the value of the variable can change, use &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this example, price1, price2, and total, are variables:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const price1 = 5;
const price2 = 6;
let total = price1 + price2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The two variables price1 and price2 are declared with the &lt;code&gt;const&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;These are constant values and cannot be changed.&lt;/p&gt;

&lt;p&gt;The variable total is declared with the &lt;code&gt;let&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;This is a value that can be changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Just Like Algebra
&lt;/h2&gt;

&lt;p&gt;Just like in algebra, variables hold values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5;
let y = 6;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like in algebra, variables are used in expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let z = x + y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the example above, you can guess that the total is calculated to be 11.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
Variables are containers for storing values.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  JavaScript Identifiers
&lt;/h2&gt;

&lt;p&gt;All JavaScript variables must be identified with unique names.&lt;/p&gt;

&lt;p&gt;These unique names are called &lt;strong&gt;identifiers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).&lt;/p&gt;

&lt;p&gt;The general rules for constructing names for variables (unique identifiers) are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Names can contain letters, digits, underscores, and dollar signs.&lt;/li&gt;
&lt;li&gt;Names must begin with a letter.&lt;/li&gt;
&lt;li&gt;Names can also begin with $ and _ (but we will not use it in this tutorial).&lt;/li&gt;
&lt;li&gt;Names are case sensitive (y and Y are different variables).&lt;/li&gt;
&lt;li&gt;Reserved words (like JavaScript keywords) cannot be used as names.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
JavaScript identifiers are case-sensitive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Assignment Operator
&lt;/h2&gt;

&lt;p&gt;In JavaScript, the equal sign &lt;code&gt;=&lt;/code&gt; is an "assignment" operator, not an "equal to" operator.&lt;/p&gt;

&lt;p&gt;This is different from algebra. The following does not make sense in algebra:&lt;/p&gt;

&lt;p&gt;x = x + 5&lt;/p&gt;

&lt;p&gt;In JavaScript, however, it makes perfect sense: it assigns the value of x + 5 to x.&lt;/p&gt;

&lt;p&gt;(It calculates the value of x + 5 and puts the result into x. The value of x is incremented by 5.)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
The "equal to" operator is written like == in JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  JavaScript Data Types
&lt;/h2&gt;

&lt;p&gt;JavaScript variables can hold numbers like 100 and text values like "John Doe".&lt;/p&gt;

&lt;p&gt;In programming, text values are called &lt;strong&gt;text strings&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;JavaScript can handle many types of data, but for now, just think of numbers and strings.&lt;/p&gt;

&lt;p&gt;=&amp;gt; Strings are written inside double or single quotes. Numbers are written without quotes.&lt;/p&gt;

&lt;p&gt;If you put a number in quotes, it will be treated as a text string.&lt;/p&gt;

&lt;p&gt;Example&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;
let person = "John Doe";
let answer = 'Yes I am!';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declaring a JavaScript Variable
&lt;/h2&gt;

&lt;p&gt;Creating a variable in JavaScript is called "declaring" a variable.&lt;/p&gt;

&lt;p&gt;You declare a JavaScript variable with the &lt;code&gt;var&lt;/code&gt; or the &lt;code&gt;let&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;After the declaration, the variable has no value (technically it is &lt;code&gt;undefined&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;To assign a value to the variable, use the equal sign:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;carName = "Volvo";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also assign a value to the variable when you declare it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let carName = "Volvo";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example below, we create a variable called &lt;code&gt;carName&lt;/code&gt; and assign the value "Volvo" to it.&lt;/p&gt;

&lt;p&gt;Then we "output" the value inside an HTML paragraph with id="demo":&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p id="demo"&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;script&amp;gt;
let carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
It's a good programming practice to declare all variables at the beginning of a script.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  One Statement, Many Variables
&lt;/h2&gt;

&lt;p&gt;You can declare many variables in one statement.&lt;/p&gt;

&lt;p&gt;Start the statement with &lt;code&gt;let&lt;/code&gt; and separate the variables by comma:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = "John Doe", carName = "Volvo", price = 200;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A declaration can span multiple lines:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = "John Doe",
carName = "Volvo",
price = 200;
Value = undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In computer programs, variables are often declared without a value. The value can be something that has to be calculated or something that will be provided later, like user input.&lt;/p&gt;

&lt;p&gt;A variable declared without a value will have the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;carName&lt;/code&gt; will have the value undefined after the execution of this statement:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Re-Declaring JavaScript Variables
&lt;/h2&gt;

&lt;p&gt;If you re-declare a JavaScript variable declared with &lt;code&gt;var&lt;/code&gt;, it will not lose its value.&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;carName&lt;/code&gt; will still have the value "Volvo" after the execution of these statements:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
You cannot re-declare a variable declared with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will not work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let carName = "Volvo";
let carName;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript Arithmetic
&lt;/h2&gt;

&lt;p&gt;As with algebra, you can do arithmetic with JavaScript variables, using operators like &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5 + 2 + 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also add strings, but strings will be concatenated:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = "John" + " " + "Doe";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also try this:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = "5" + 2 + 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
If you put a number in quotes, the rest of the numbers will be treated as strings, and concatenated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now try this:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 2 + 3 + "5";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript Dollar Sign $
&lt;/h2&gt;

&lt;p&gt;Since JavaScript treats a dollar sign as a letter, identifiers containing $ are valid variable names:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let $ = "Hello World";
let $$$ = 2;
let $myMoney = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the dollar sign is not very common in JavaScript, but professional programmers often use it as an alias for the main function in a JavaScript library.&lt;/p&gt;

&lt;p&gt;In the JavaScript library jQuery, for instance, the main function $ is used to select HTML elements. In jQuery $("p"); means "select all p elements".&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Underscore (_)
&lt;/h2&gt;

&lt;p&gt;Since JavaScript treats underscore as a letter, identifiers containing _ are valid variable names:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let _lastName = "Johnson";
let _x = 2;
let _100 = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the underscore is not very common in JavaScript, but a convention among professional programmers is to use it as an alias for "private (hidden)" variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Scope
&lt;/h2&gt;

&lt;p&gt;Scope determines the accessibility (visibility) of variables.&lt;/p&gt;

&lt;p&gt;JavaScript has 3 types of scope:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block scope&lt;/li&gt;
&lt;li&gt;Function scope&lt;/li&gt;
&lt;li&gt;Global scope&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Block Scope
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; provide Block Scope in JavaScript.&lt;/p&gt;

&lt;p&gt;Variables declared inside a &lt;code&gt;{ }&lt;/code&gt; block cannot be accessed from outside the block:&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  let x = 2;
}
// x can NOT be used here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variables declared with the var keyword can NOT have block scope.&lt;/p&gt;

&lt;p&gt;Variables declared inside a &lt;code&gt;{ }&lt;/code&gt; block can be accessed from outside the block.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  var x = 2;
}
// x CAN be used here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Local Scope
&lt;/h2&gt;

&lt;p&gt;Variables declared within a JavaScript function, become LOCAL to the function.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Local variables have Function Scope:&lt;/p&gt;

&lt;p&gt;They can only be accessed from within the function.&lt;/p&gt;

&lt;p&gt;Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.&lt;/p&gt;

&lt;p&gt;Local variables are created when a function starts, and deleted when the function is completed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Scope
&lt;/h2&gt;

&lt;p&gt;JavaScript has function scope: Each function creates a new scope.&lt;/p&gt;

&lt;p&gt;Variables defined inside a function are not accessible (visible) from outside the function.&lt;/p&gt;

&lt;p&gt;Variables declared with &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are quite similar when declared inside a function.&lt;/p&gt;

&lt;p&gt;They all have Function Scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  var carName = "Volvo";   // Function Scope
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  let carName = "Volvo";   // Function Scope
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  const carName = "Volvo";   // Function Scope
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Global JavaScript Variables
&lt;/h2&gt;

&lt;p&gt;A variable declared outside a function, becomes GLOBAL.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let carName = "Volvo";
// code here can use carName

function myFunction() {
// code here can also use carName
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A global variable has Global Scope:&lt;/p&gt;

&lt;p&gt;All scripts and functions on a web page can access it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Global Scope
&lt;/h2&gt;

&lt;p&gt;Variables declared Globally (outside any function) have Global Scope.&lt;/p&gt;

&lt;p&gt;Global variables can be accessed from anywhere in a JavaScript program.&lt;/p&gt;

&lt;p&gt;Variables declared with var, let and const are quite similar when declared outside a block.&lt;/p&gt;

&lt;p&gt;They all have Global Scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 2;       // Global scope
let x = 2;       // Global scope
const x = 2;       // Global scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Points to Remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Variable stores data value that can be changed later on.&lt;/li&gt;
&lt;li&gt;Variables can be defined using var keyword. Variables defined without var keyword become global variables.&lt;/li&gt;
&lt;li&gt;Variables must be initialized before accessing it.&lt;/li&gt;
&lt;li&gt;JavaScript allows multiple white spaces and line breaks in a variable declaration.&lt;/li&gt;
&lt;li&gt;Multiple variables can be defined in a single line separated by a comma.&lt;/li&gt;
&lt;li&gt;JavaScript is a loosely-typed language, so a variable can store any type value.&lt;/li&gt;
&lt;li&gt;Variable names are case-sensitive.&lt;/li&gt;
&lt;li&gt;Variable names can contain letters, digits, or the symbols $ and _. It cannot start with a digit 0 - 9.&lt;/li&gt;
&lt;li&gt;Variables can have local or global scope. Local variables cannot be accessed out of the function where they are declared, whereas the global variables can be accessed from anywhere.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Functions In JavaScript</title>
      <dc:creator>yevastepanyan</dc:creator>
      <pubDate>Thu, 10 Nov 2022 18:23:32 +0000</pubDate>
      <link>https://dev.to/yevastepanyan/functions-in-javascript-1c39</link>
      <guid>https://dev.to/yevastepanyan/functions-in-javascript-1c39</guid>
      <description>&lt;h2&gt;
  
  
  JavaScript Functions
&lt;/h2&gt;

&lt;p&gt;A JavaScript function is a block of code designed to perform a particular task.&lt;/p&gt;

&lt;p&gt;A JavaScript function is executed when "something" invokes it (calls it).&lt;/p&gt;

&lt;p&gt;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 to compute the product of p1 and p2
function myFunction(p1, p2) {
  return p1 * p2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JavaScript Function Syntax
&lt;/h2&gt;

&lt;p&gt;A JavaScript function is defined with the &lt;code&gt;function&lt;/code&gt; keyword, followed by a name, followed by parentheses ().&lt;/p&gt;

&lt;p&gt;Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).&lt;/p&gt;

&lt;p&gt;The parentheses may include parameter names separated by commas:&lt;br&gt;
(parameter1, parameter2, ...)&lt;/p&gt;

&lt;p&gt;The code to be executed, by the function, is placed inside curly brackets: &lt;code&gt;{}&lt;/code&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 name(parameter1, parameter2, parameter3) {
  // code to be executed
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Function parameters are listed inside the parentheses () in the function definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function arguments&lt;/strong&gt; are the values received by the function when it is invoked.&lt;/p&gt;

&lt;p&gt;Inside the function, the arguments (the parameters) behave as local variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Invocation
&lt;/h2&gt;

&lt;p&gt;The code inside the function will execute when "something" invokes (calls) the function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When an event occurs (when a user clicks a button)&lt;/li&gt;
&lt;li&gt;When it is invoked (called) from JavaScript code&lt;/li&gt;
&lt;li&gt;Automatically (self invoked)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will learn a lot more about function invocation later in this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Return
&lt;/h2&gt;

&lt;p&gt;When JavaScript reaches a &lt;code&gt;return&lt;/code&gt; statement, the function will stop executing.&lt;/p&gt;

&lt;p&gt;If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.&lt;/p&gt;

&lt;p&gt;Functions often compute a return value. The return value is "returned" back to the "caller":&lt;/p&gt;

&lt;p&gt;A function can return zero or one value using return keyword.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
Return value From a Function&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(val1, val2) {
    return val1 + val2;
};

var result = Sum(10,20); // returns 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example&lt;br&gt;
Calculate the product of two numbers, and return the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = myFunction(4, 3);   // Function is called, return value will end up in x

function myFunction(a, b) {
  return a * b;             // Function returns the product of a and b
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result in x will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Functions?
&lt;/h2&gt;

&lt;p&gt;You can reuse code: Define the code once, and use it many times.&lt;/p&gt;

&lt;p&gt;You can use the same code many times with different arguments, to produce different results.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
Convert Fahrenheit to Celsius:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius(77);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;()&lt;/code&gt; Operator Invokes the Function&lt;br&gt;
Using the example above, &lt;code&gt;toCelsius&lt;/code&gt; refers to the function object, and &lt;code&gt;toCelsius()&lt;/code&gt; refers to the function result.&lt;/p&gt;

&lt;p&gt;Accessing a function without &lt;code&gt;()&lt;/code&gt; will return the function object instead of the function result.&lt;/p&gt;

&lt;p&gt;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 toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Functions Used as Variable Values
&lt;/h2&gt;

&lt;p&gt;Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
Instead of using a variable to store the return value of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = toCelsius(77);
let text = "The temperature is " + x + " Celsius";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the function directly, as a variable value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = "The temperature is " + toCelsius(77) + " Celsius";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Local Variables
&lt;/h2&gt;

&lt;p&gt;Variables declared within a JavaScript function, become &lt;strong&gt;LOCAL&lt;/strong&gt; to the function.&lt;/p&gt;

&lt;p&gt;Local variables can only be accessed from within the function.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.&lt;/p&gt;

&lt;p&gt;Local variables are created when a function starts, and deleted when the function is completed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Functions
&lt;/h2&gt;

&lt;p&gt;In JavaScript, a function can have one or more inner functions. These nested functions are in the scope of outer function. &lt;br&gt;
Inner function can access variables and parameters of outer function. However, outer function cannot access variables defined inside inner functions.&lt;/p&gt;

&lt;p&gt;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 ShowMessage(firstName){
    function SayHello() {
        alert("Hello " + firstName);
    }
    return SayHello();
}
ShowMessage("Steve");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Points to Remember :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript a function allows you to define a block of code, give it a name and then execute it as many times as you want.&lt;/li&gt;
&lt;li&gt;A function can be defined using function keyword and can be executed using () operator.&lt;/li&gt;
&lt;li&gt;A function can include one or more parameters. It is optional to specify function parameter values while executing it.&lt;/li&gt;
&lt;li&gt;JavaScript is a loosely-typed language. A function parameter can hold value of any data type.&lt;/li&gt;
&lt;li&gt;You can specify less or more arguments while calling function.&lt;/li&gt;
&lt;li&gt;All the functions can access arguments object by default instead of parameter names.&lt;/li&gt;
&lt;li&gt;A function can return a literal value or another function.&lt;/li&gt;
&lt;li&gt;A function can be assigned to a variable with different name.&lt;/li&gt;
&lt;li&gt;JavaScript allows you to create anonymous functions that must be assigned to a variable.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Binary Numbers: Introduction and Arithmetic Operations</title>
      <dc:creator>yevastepanyan</dc:creator>
      <pubDate>Tue, 20 Sep 2022 04:06:56 +0000</pubDate>
      <link>https://dev.to/yevastepanyan/binary-numbers-introduction-and-arithmetic-operations-3nlg</link>
      <guid>https://dev.to/yevastepanyan/binary-numbers-introduction-and-arithmetic-operations-3nlg</guid>
      <description>&lt;h2&gt;
  
  
  What is Binary?
&lt;/h2&gt;

&lt;p&gt;The word binary, in general, means something that involves two things. So Binary Number System is a system of numbers that has ... yes, two numbers. In other words, binary is a base-2 number system, which only uses two digits - 0 &amp;amp; 1.&lt;/p&gt;

&lt;p&gt;In everyday life, we people use and understand the decimal system (0 to 9). You may ask that if people created computers, how come the computers only know two numbers instead of ten? The answer is straightforward. It turns out, guys, computers can only understand two states - whether there is electricity or there is not. 1 and 0 represent the presence or the absence of electricity, respectively. You know what that means, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Where do we use binary?
&lt;/h2&gt;

&lt;p&gt;The binary system represents a number in terms of only two numbers, 0 and 1. A number in a decimal numeral system can be represented like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yxcR0R5M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kfswlib3o86hf2e6pqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yxcR0R5M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kfswlib3o86hf2e6pqw.png" alt="3654indecimal" width="767" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, 10 is the base, as the number is represented using powers of 10. So if we want to represent the same number in a binary,  we need powers of 2, t - logic.&lt;br&gt;
Do you know how to convert the number 3654 from decimal to binary? Keep reading. After a few paragraphs, you'll get there. Maybe.&lt;/p&gt;

&lt;h2&gt;
  
  
  Converting from decimal to binary
&lt;/h2&gt;

&lt;p&gt;As you already know, a binary number is an addition of powers of 2. But how do we exactly represent the number? Okay, first of all, let's pick a small number — 5, for example.&lt;/p&gt;

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

&lt;p&gt;We've shown it as a sum. It's time to use our 1s and 0s. In the case of 5, it's 1*4 + 0*2 + 1*1. All we need is a number consisting of only 0s and 1s that expresses 1*4 + 0*2 + 1*1. Wow, we're definitely going somewhere with this. Look at the example of 3654. In the same way, in binary, it's&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ROfAC_Kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38li9qlnzsgrcdfk1tss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ROfAC_Kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38li9qlnzsgrcdfk1tss.png" alt="101" width="504" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take another example. 87 now. Now it's harder to find powers of 2; add them together and get 87. Here's an easy technique for that. Look at the first several powers of 2&lt;/p&gt;

&lt;p&gt;Now, subtract the greatest power of 2 that is not greater than 87. It's 64. &lt;/p&gt;

&lt;p&gt;87 - 64 = 23&lt;/p&gt;

&lt;p&gt;Do the same thing with 23. It's 16 for 23.&lt;/p&gt;

&lt;p&gt;23 - 16 = 7&lt;/p&gt;

&lt;p&gt;Do this again,&lt;/p&gt;

&lt;p&gt;7 - 4 = 3&lt;/p&gt;

&lt;p&gt;And again...&lt;/p&gt;

&lt;p&gt;3 - 2 = 1&lt;/p&gt;

&lt;p&gt;And finally, &lt;/p&gt;

&lt;p&gt;1 - 1 = 0&lt;/p&gt;

&lt;p&gt;You got that &lt;/p&gt;

&lt;p&gt;87 = 64 + 16 + 4 + 2 + 1 &lt;/p&gt;

&lt;p&gt;Starting from the right and considering the first place as the least power of 2 (the same as 1), we write 1 if there is a specific power of 2 present in the sum and 0 if not. To be more precise, here is a template.&lt;/p&gt;

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

&lt;p&gt;Fill in the spaces, and you get&lt;/p&gt;

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

&lt;p&gt;Indeed,&lt;/p&gt;

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

&lt;p&gt;Congrats, now you know the conversion from decimal to binary. Conversion from binary to decimal is the same. &lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Operations with Binary Numbers
&lt;/h2&gt;

&lt;p&gt;As in any number system, we can add, subtract, multiply and divide binary numbers. Let's take a look at each of the operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Addition
&lt;/h2&gt;

&lt;p&gt;The addition of binary numbers takes place the same way as that of decimals... almost. In the case of binary, we use this table.&lt;/p&gt;

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

&lt;p&gt;Not very clear, I know. You're probably wondering what the hell is 1+1=0 and carry 1. Let me explain with an example.&lt;/p&gt;

&lt;p&gt;For instance, we try to add 1010 and 111.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Write the numbers in columns, same place digits in the same column like&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Starting from the right column, add the numbers of each column.&lt;/p&gt;

&lt;p&gt;2.1) For the first column, we have 0+1, which is equal to 1 according to our table.&lt;/p&gt;

&lt;p&gt;2.2) Next comes 1+1. We know that 1+1 equals 0, and the carry is 1. So we write 0 and write a tiny 1 on the top of the next column.&lt;/p&gt;

&lt;p&gt;2.3) The next step is adding 0+1, but oh wait, we also have a carry! Basically it's 1+(0+1) = 1+1 = 0 and 1 as a carry. AGAIN.&lt;/p&gt;

&lt;p&gt;2.4) Finally, we have just a 1 in the last column. Should we just right it down? Nope. We forgot about the carry. This means we have 1 + 1, which is 0 (and a carry 1).&lt;/p&gt;

&lt;p&gt;2.5) The only thing left is the last carry, which we right down.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Multiplication
&lt;/h2&gt;

&lt;p&gt;Multiplication of binary is even simpler if you know addition. &lt;br&gt;
Here's the table for multiplication&lt;/p&gt;

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

&lt;p&gt;Again, let's write an example. 1001 x 111 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Write the numbers in columns, just like during addition.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Multiply the second number's first (right) digit with the first number. If it is 1, copy the first number. If it is 0, write 0s.&lt;br&gt;
In our example, it's 1, so we write 1001 in the first row.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; Multiply the second digit of the second number with the first number. Write the result by moving 1 column left. So it's 0000 but starting from the next higher order digit like&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;4)&lt;/strong&gt; Repeat the same action until the digits of the second number are over. The product obtained in each row is called the partial product.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5)&lt;/strong&gt; Finally, add all the partial products. To add all the binary numbers, use the rules of binary addition.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Subtraction
&lt;/h2&gt;

&lt;p&gt;Capture the table for subtraction&lt;/p&gt;

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

&lt;p&gt;We're now doing subtraction using the same numbers in the multiplication example.&lt;/p&gt;

&lt;p&gt;Guess the first step. &lt;br&gt;
&lt;strong&gt;1)&lt;/strong&gt; Write the numbers in columns&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Start to subtract numbers starting from the right column and using the subtraction table rules.&lt;br&gt;
2.1) For the first and second columns, 1-1 = 0 and 0-0 = 0&lt;br&gt;
2.2) In the third column, we have 0-1, which equals 1, but we have to borrow a 1 from the next higher order digit.&lt;br&gt;
2.3) We have nothing left in the last column because we have borrowed 1 from there.&lt;/p&gt;

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

&lt;p&gt;Thank you for reading; I hope this was helpful. Have a great day!&lt;/p&gt;

</description>
      <category>binary</category>
      <category>beginners</category>
      <category>dearkhachaturimterriblysorry</category>
      <category>forpostingafterthdeadline</category>
    </item>
  </channel>
</rss>
