Posts

Showing posts from January, 2024

Functions in JavaScript: Closures in JavaScript:

Image
  Functions in Javascript: There may be a certain piece of code that you would want to run multiple times in your program. In such cases, instead of writing the same code several times, it is better to wrap that code inside something called a function and then reuse that function as and when needed. Functions can be defined in Javascript using the following syntax: function  function_name  (parameters) { lines of code  }   We use the  function  keyword followed by the function name of our own choice. Parameters are certain values which may be required inside a function. These values are passed while calling that function. Note: Parameters are optional, a functional may or may not have parameters.   What does calling the function mean? Calling a function simply means using that function whenever needed. It is also called invoking a function.  Examples: Function with parameters: Function without parameters: Returning values from a function: Sup...

Data types in JavaScript: Difference between ‘==’ and ‘===’ in Javascript:

Image
  Data types in JavaScript: Data types are nothing but different types of data that can be stored. JavaScript provides two types of data types: Primitive and Non-primitive. It is important to note that JavaScript is a dynamically typed language. It means that we do not have to specify the data type for a variable, it is done dynamically by the JavaScript engine.   1. Primitive Data Types: Number : It represents numeric values, both with and without decimal. Eg: let x = 5;   String : It represents a sequence of characters inside quotes. It can consist of letters, words, and sentences.  It is important to note that even numbers enclosed inside quotes are treated as strings. Eg: let x = "Test123";   Boolean : It represents either true or false. Eg: let a = true;   Undefined : A variable that has not been assigned a value is of type undefined. Eg: let a;   null : It represents null, which means no value at all. Eg: let a = null;   2. Non-primitive dat...

else-if condition in if-else statements: Concatenation in JavaScript :

Image
  else-if condition in if-else statements: While specifying conditions for if-else statements, there may arise a need to include more than just two scenarios. For example, suppose we have a variable ‘n’ and we have to check if it is positive or negative. JS code for this problem would look like this: Quite simple, right? However, suppose we twist this problem a bit. If we have to check if the value of ‘n’ is positive, negative, OR zero. Can we do it using only if-else?  No, right? Here comes the scenario where we have to check more than two conditions and give output for each. This is where else-if comes into the game.  Structure of if else-if else looks something like this: If (condition 1)  { statements } else if (condition 2) { statements } else  { statements }   Working for the above code is explained as follows: Condition 1 is checked first. If it is true, statements inside the first block are executed and the remaining conditions are not checked. If t...

Modulo operator: Scope of let and const: What is a block?

Image
  Modulo operator: Modulo operator (%) is used to find the remainder when two numbers are divided. Example: Scope of let and const: One of the major differences between var and let/const is the scope. However, one might wonder what scope exactly is. The scope of a variable is the segment of the code where the variable can be accessed. Variables declared using var have the global scope whereas let and const are blocks scoped. Here, global scope means the value of a variable is accessible globally within the code. On the other hand, block scope means the value of the variable is accessible only within the block where it is declared.      What is a block?   A block is nothing but a set of statements enclosed inside curly braces. It is also called a Compound Statement. It is used whenever we want to execute several lines whereas JS allows only one. For example, we can use a block after an if condition to execute the block code conditionally.      Exam...

JavaScript as a scripting language:

  JavaScript as a scripting language: JavaScript (also known as JS) is a scripting language, but what does scripting language mean?  A scripting language is a programming language that allows some control of another software application(s). In the case of JavaScript, this application is the browser.  JavaScript is actually an interpreted language and not a compiled language.  The compilation is the process of converting high-level programming languages like Java, C++, etc. into machine code that a computer can execute. In the case of JavaScript, an interpreter in the browser interprets each line of JavaScript code and runs it. JavaScript can "display" data in different ways: Writing into an HTML element, using  innerHTML . Writing into the HTML output using  document.write() . Writing into an alert box, using  window.alert() . Writing into the browser console, using  console.log() .