Hey, I'm Nicholas in this post i will be impacting knowledge on some important things you need to know about Javascript, go grab a coffee let's ride.
SECTION 1
Javascript Data Types
In Javascript, there are five (5) basic, or primitive, types of data, which include
Strings : A string is a collection of alphanumeric characters. OR,
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols.
Example: let string = "32";
let string = " Am Happy";
You can use quotes inside a string, as long as they don't match the quotes surrounding the string: let string = It's coffee time
Numbers: Let’s talk about numbers. Numbers are as straightforward as they sound. So numbers are any integer or decimal number created in the language, and they’re used for money, age, etc.
Example: let x = 3.14; // A number with decimals
let y = 3; // A number without decimals
Booleans: Booleans have two values. True and False. When we create a boolean, we’re simply saying it’s true or it’s false. OR, A Boolean value is one that can either be TRUE or FALSE. If you need to know “yes” or “no” about something, then you would want to use the boolean function.
const onLight= false;
onLight = true;
onLight;
Output = true
In the above example, the variable "onLight" being set to "true" would indicate that the lights are on. If it was set to "false" then that would mean they are off.
It’s useful to store booleans in variables to keep track of their values and change them over time. Booleans are used as functions to get the values of variables, objects, conditions, and expressions. In fact, Booleans are critical for conditionals to work.
Undefined:
The undefined property indicates that a variable has not been assigned a value, or not declared at all.
Example :
const n;
console.log(n);
Output = Udefined
In the above example, the variable "n" was not assigned to a value.
Array
In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index. And JavaScript array has the following characteristics: First, an array can hold values of mixed types. For example, you can have an array that stores elements with the types number, string, and boolean.
Example:
const arr = [1, "coffee", false];
SECTION 2
Hoisting
What is Hoisting in Javascript?
Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
Hoisting allows functions to be safely used in code before they are declared.
Variable and class declarations are also hoisted, so they too can be referenced before they are declared. Note that doing so can lead to unexpected errors, and is not generally recommended.
console.log(hoist); // Returns 'undefined' from hoisted const declaration (not 6)
const num = 6; // declaration.
console.log(hoist); // Returns 6 after the line with declaration is executed.
If we forget the declaration altogether (and only initialize the value) the variable isn't hoisted.
SECTION 3
In this Section we will get to know the difference between “==” and “===” operators. And other things Let's Ride
The difference between == and === is that:
== converts the variable values to the same type before performing comparison. This is called type coercion.
=== does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.
Example
const dev = 2;
const dev_again = 2;
const dev_string = "2"; // note: this is string
console.log(dev == one_again); // true
console.log(dev === one_again); // true
console.log(dev == one_string); // true. See below for explanation.
console.log(dev === one_string); // false. See below for explanation.
console.log(one == one_string)
returns true because both variables, dev and dev_string contain the same value even though they have different types: "dev" is of type Number whereas dev_string is String. But since the == operator does type coercion, the result is true.
console.log(one === one_string)
returns false because the types of variables are different.
Let's talk about Implicit Type Coercion in javascript.
What is Implicit Type Coercion?
Implicit coercion happens when JavaScript coerces the value type to the expected type under the hood. This type of coercion happens without the developer noticing.
OR
implicit coercion simply refers to Javascript attempting to coerce an unexpected value type to the expected type. So you can pass a string where it expects a number, an object where it expects a string etc, and it will try to convert it to the right type.
Yeah i was going through a post on Twitter, and the Author of the post asked: Is javascript a statically typed or a dynamically typed language so i will be answering that question here grab your coffee let's kick off.
Javascript is a not a Statically typed language yes is not, javascript is a dynamic language because it doesn't just have a few dynamic aspects, pretty much everything is dynamic. All variables are dynamic (both in type and existence), and even the code is dynamic. You can create new variables at runtime, and the type of variables is determined at runtime.
You can create new functions at any time, or replace existing functions. When used in a browser, code is added when more script files are loaded, and you can load more files any time you like.
Nowadays JavaScript is compiled in many implementations, and static code and static types are generated in the background. However, the behaviour is still dynamic, the compiler only generates static types when it finds that the dynamic aspects are not used for a specific object.
- What is NaN property in JavaScript?
In JavaScript, NaN
stands for Not a Number. It represents a value which is not a valid number. It can be used to check whether a number entered is a valid number or not a number. To assign a variable to NaN value, we can use one of the two following ways.
var a = NaN
var a = Number.NaN
Explaining what passed by value and passed by reference means.
Pass By Value
Pass by value refers to a mechanism of copying the function parameter value to another variable.
OR
Pass by value means that a copy of the actual parameter's value is made in memory, i.e. the caller and callee have two independent variables with the same value.If the callee modifies the parameter value, the effect is not visible to the caller.
Pass by reference
Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller’s variable.
When to use pass by value?
If you are building multi-threaded application, then you don’t have to worry of objects getting modified by other threads. In distributed application pass by value can save the over network overhead to keep the objects in sync.
When to use pass by reference?
In pass by reference, no new copy of the variable is made, so overhead of copying is saved. This makes programs efficient especially when passing objects of large structs or classes.
What is an Immediately Invoked Function in javascript
An Immediately-invoked Function Expression is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations.
This is the syntax that defines an IIFE:
(function() {
/* */
})()
IIFEs can be defined with arrow functions as well:
(() => {
/* */
})()
Explaining Higher Order Functions in javascript.
In Javascript, functions can be assigned to variables in the same way that strings or arrays can. They can be passed into other functions as parameters or returned from them as well. A “higher-order function” is a function that accepts functions as parameters and/or returns a function.
Let's talk talk about this
keyword in javascript
JavaScript this keyword
this
keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.
Method
let car = {
brand: 'Honda',
getBrand: function () {
return this.brand;
}
}
console.log(car.getBrand()); // Honda
In this example, the this
object in the getBrand() method references the car object.
Explaining call(), apply() and, bind() methods.
The call() and apply() are very similar methods. They both execute the bound function on the object immediately. The bind() method does not execute the function right away. Instead, it creates and returns a bound function that can be executed later.
Currying in JavaScript
Currying is a process in functional programming in which we
can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.
In other words, when a function, instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.
That is, when we turn a function call sum(3,6,7)
into sum(3)(6)(7)
Explain Scope and Scope Chain in javascript
Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global: Global variables are those declared outside of a block. Local variables are those declared inside of a block. While The scope chain is used to resolve the value of variable names in javascript. Without a scope chain the Javascript engine wouldn't know which value to pick for a certain variable name if there are multiple defined at different scopes.
Explaining Closures in JavaScript
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
What are object prototypes
Prototypes are the mechanism by which JavaScript objects inherit features from one another. In this article, we explain what a prototype is, how prototype chains work, and how a prototype for an object can be set.
What are callbacks? in Javascript
A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.
What is memoization
Memorization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
Memoizing in simple terms means memorizing or storing in memory. A memoized function is usually faster because if the function is called subsequently with the previous value(s), then instead of executing the function, we would be fetching the result from the cache.
What is recursion in a programming language
Recursion means "defining a problem in terms of itself". This can be a very powerful tool in writing algorithms. Recursion comes directly from Mathematics, where there are many examples of expressions written in terms of themselves. For example, the Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)
What is the use of a constructor function in javascript
A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.
What is DOM?: Document Object Model
The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.
A web page is a document that can be either displayed in the browser window or as the HTML source. In both cases, it is the same document but the Document Object Model (DOM) representation allows it to be manipulated. As an object-oriented representation of the web page, it can be modified with a scripting language such as JavaScript.
Top comments (0)