JavaScript Basics: All You need to Know

  • Javascript is browser-compatible, and it supports all types of browsers like Chrome, Firefox, Safari, etc. making it more versatile for web development Javascript is used with frameworks and libraries, which makes tasks simpler and provides more functionalities. Ex-Reactjs , Angular and Vuejs

  • It supports Asynchronous Programming that includes features like callback, promise, and async / await and we can do all the possible things without blocking the main execution thread

  • It offers you the freedom to manage events through event handling, which is also referred to as mouse clicking, mouse movement, form submission, and other activities.

  • We can modify the document object model (DOM) in a dynamic manner, like changing the context, structure, and style sheet of any web page

“In this section, let’s review all the fundamentals before moving on to the intermediate and advanced levels. And learn what really matters, because your time is important Thus, please have your coffee at your table. “

  • How to do a Comparison in JavaScript

Most of the time, we are making these type of comparison, which is not a good approach

console.log(null > 0); //false
console.log(null == 0); //false
console.log(null >= 0); //true

Reasons: The reason is that an equality check == and comparison > < >= <= work differently because comparison converts null to a number.

treating it as 0, which is why null >= 0 is true and null > 0 is false

  • Conversion and Datatypes

In JavaScript, we can dynamically change the datatype of a variable. This flexibility is possible because JavaScript uses dynamic typing, allowing variables to hold values of different types throughout the execution of your program. Let’s see how we can convert datatypes

suppose we have:

let name = 'devcodestack';
console.log('old datatype is', typeof name);  //output - old datatype is string

Now changing the datatype of ‘name’ variable

let changedType = Number(name);
console.log('new datatype is ', typeof changedType);   // output - new datatype is  number

NOTE: Everything is true (also the true value itself) in Boolean, except values like –

False : Boolean value false itself.

0 : The number zero.

-0 : A negative zero.

0n : stands for BigInt zero, which is used to handle huge numbers and was introduced in ES2020.

“” : denotes an empty string with no characters.

Null: Indicates the lack of any value or object.

Undefined :  Refers to a variable that has not been assigned a value or a property that does not exist.

NaN : An abbreviation for “Not a Number,” which is commonly returned when a mathematical operation fails to provide a result.

Operations in Javascript

Datatype

Datatype refers to the data type a variable may hold. JavaScript provides both primitive and non-primitive data types.

Primitive

Number: represents numeric values, which can be integers (whole numbers) or floating-point numbers (decimal numbers).

String: represents textual data, which is enclosed within either single quotes (”), double quotes (“”), or backticks (“).

Boolean: represents a logical value, which can be either true or false.

Undefined: represents a variable that has been declared but hasn’t been assigned any value.

Null represents the deliberate absence of any value or object reference.

Symbol (added in ES6): This represents unique identifiers, often used as keys in objects.

BigInt: BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1.

Non-Primitive

Object: represents a collection of key-value pairs, where values can be of primitive or non-primitive data types.

Function: represents a reusable block of code that can be called to perform a specific task.

Array: represents an ordered collection of values, which can be of the same data type or mixed data types.

RegExp: The RegExp Object is a regular expression with additional properties and methods and is used for matching text with a pattern.

Date: They represent dates and times and can be created using the Date constructor.

PrimitiveNon-Primitive
Primitive Data types are predefined. Non-Primitive data types are created by the programmer
Examples include numbers and strings.Examples are arrays and linked lists.
Primitive Data types will have certain valuesNon-Primitive data types can be NULL.
size depends on the type of data structure.size is not fixed
It can start with a lowercase letter.It can start in uppercase.

Stack memory is used in primitive datatypes, & Heap memory is used in non-primitive datatypes

Stack Memory and Primitive Data Types

Stack memory is utilized for storing primitive data types, which include numbers, strings, booleans, null, undefined, and symbols. Primitive data types are immutable, meaning once their value is set, it cannot be changed. When you assign a primitive value to a variable, the value itself is stored in the stack.

let myName = 'devcodestack';
let anotherName = myName;

anotherName = 'codestack';

console.log(anotherName);   // Output: codestack
console.log(myName);        // Output: devcodestack
  • myName is assigned the string ‘devcodestack’.
  • anotherName is then assigned the value of myName. This means anotherName gets its own copy of the string stored in the stack.
  • When anotherName is changed to ‘codestack’, it does not affect myName because primitives are stored by value. Each variable holds a copy of the value.

Heap Memory and Non-Primitive Data Types

Heap memory is used for storing non-primitive data types, such as objects and arrays. These types are mutable and are stored by reference. When you assign an object or array to a variable, the variable holds a reference to the memory location where the object or array resides, not the actual data.

let userDetail = {
  name: 'Devcode',
  email: 'devcode@gmail.com',
  phone: 12345566,
};

let newUserDetail = userDetail;

newUserDetail.email = 'stack@gmail.com';

console.log(userDetail.email);          // Output: stack@gmail.com
console.log(newUserDetail.email);       // Output: stack@gmail.com
  • userDetail is an object stored in heap memory.
  • newUserDetail is assigned a reference to the same object in heap memory.
  • Modifying the email property of newUserDetail affects userDetail as well because both variables reference the same object in memory.

Stack Memory: Utilized for primitive data types. Variables contain the actual values, and each variable has its own copy. Changes to one variable do not impact others.

Heap Memory: Utilized for non-primitive data types. Variables contain references to objects or arrays. Changes made through one reference affect all references to the same object or array.

You May Also Like

More From Author

1 Comment

Add yours
  1. 1
    Raymondflasp

    Good morning! devcodestack.com

    Did you know that it is possible to send appeals utterly lawful? We tender a new legal way of sending commercial offers through contact forms.
    It’s unlikely for messages sent via Feedback Forms to end up in spam, as they are seen as significant.
    Come and give our service a try – it’s free!
    You can count on us for sending up to 50,000 messages.

    The cost of sending one million messages is $59.

    This offer is automatically generated.
    Please use the contact details below to get in touch with us.

    Contact us.
    Telegram – https://t.me/FeedbackFormEU
    Skype live:feedbackform2019
    WhatsApp +375259112693
    WhatsApp https://wa.me/+375259112693

    We only use chat for communication.

+ Leave a Comment