# JavaScript as a programming language

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>software is nothing more than data plus instructions.</em></strong></div>
</div>

### What is the programming language?

Programming language is just a tool that allows us to write code that will instruct a computer to do something.

### What is the JavaScript?

`JavaScript` is a `high level` language, which means that we don’t have to think about a lot of `complex stuff` such managing `computer’s memory` while it runs the program.

There are a lot of so-called `abstractions` over all these small details that we don’t want worry about. And this makes the language a lot easier to write and to learn.

It’s Object Oriented(Based on objects for storing most kind of data) and multi-paradigm(Can use different programming styles) language.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>JavaScript is case-sensitive</em></strong></div>
</div>

![](https://cdn.hashnode.com/uploads/covers/695114b01f48b622b5631972/3907e934-c514-4164-abb7-a2f1d5398cf9.webp align="center")

### **Popularity of JavaScript**

![](https://cdn.hashnode.com/uploads/covers/695114b01f48b622b5631972/cf022e2a-2ef2-4b94-8e85-dfec23e692ab.webp align="center")

### **Dynamic Typing**

JavaScript has a feature called `dynamic typing`. It means when you create a `new variable`, you don’t have to `manually define the data type` of the value that it contains.

JavaScript automatically determines the `data type` of the value when it’s stored into a variable. In JavaScript that **value** has a **type** not **variable**.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>It’s simply mean that we can easily change the </em></strong><code>type</code><strong><em> of a value that is hold by a variable.</em></strong></div>
</div>

### ***Programming Paradigms in JavaScript***

*JavaScript supports both* `imperative` *and* `declarative` *programming styles:*

*   **Imperative Programming** : *Focuses on how to perform tasks by controlling the* `flow` *of computation.* This includes approaches like `procedural` and `object-oriented` programming, often using constructs like `async/await` to handle asynchronous actions.
    
*   **Declarative Programming** : *Focuses on what should be done rather than how it’s done.* It emphasizes describing the desired result, such as with `arrow functions`, without detailing the steps to achieve it.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>variable, data types, conditionals, loops, functions, and objects are the LEGO of any programming language.</em></strong></div>
</div>

* * *

### **Variables**

Variable can be said as ***container / label*** to contain some value. To store some value in ***memory,*** the variable is used for it.

*   Variables can be declared using `var`, `let`, or `const`
    
*   JavaScript is ***dynamically typed***, so `types of values` are decided at ***runtime***.
    
*   You don’t need to specify a ***data type*** when creating a variable.
    
*   `var (function and global scoped), let and const (local scoped).`
    
*   `var` can be `re-declared` in the same scope, but `let` and `const` cannot be re-declared.
    

```javascript
var x = 10;
var x = 20; // Allowed

let y = 30;
let y = 40; // SyntaxError

const z = 50;
const z = 60; // SyntaxError
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">We can change the elements of <code>array</code> or <code>objects</code> even if declared as <code>const</code>. <code>How?</code> Because their reference is saved in <code>stack</code> while value in <code>heap</code>.</div>
</div>

```javascript
let name = "JS";
```

![](https://cdn.hashnode.com/uploads/covers/695114b01f48b622b5631972/2221fba6-d57d-4ea2-826a-f30f1c3492be.png align="center")

### ***Rules for Naming Variables***

When naming variables in JavaScript, follow these rules

*   Variable names must begin with a `letter (A to Z or a to z)`, `underscore (_)`, or `dollar sign ($)`.
    
*   ***Subsequent*** (after that) characters can be ***letters***, ***numbers***, ***underscores***, or ***dollar signs***.
    
*   Variable names are ***case-sensitive*** (e.g., `age` and `Age` are different variables).
    
*   Reserved keywords (like `function`, `class`, `return`, etc.) cannot be used as variable names.
    

### ***Data Types***

*It’s an attribute (quality) that identifies (recognizes) a piece of data and instructs (tells) a computer system on how to interpret (explain) its value is called a* `data type`*.*

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Each data type has its own methods and operations that control how it can be used.</em></strong></div>
</div>

> The **data** has type (e.g. **number**, **string**, **boolean** etc.) is **data type**.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>Primitive data types in JavaScript represent </em><code>simple</code><em>, </em><code>immutable</code><em> values stored directly in </em><code>memory</code><em>, ensuring efficiency in both memory usage and performance.</em></div>
</div>

![](https://cdn.hashnode.com/uploads/covers/695114b01f48b622b5631972/76a38a6d-5e1b-479e-a041-403e26f3123d.webp align="center")

### ***Data-types in JavaScript***

### 1\. `Number`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>The</em><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-numbers/" style="pointer-events: none;"><strong><em><u>&nbsp;Number</u></em></strong></a><em>&nbsp;data type in JavaScript includes both </em><code>integers</code><em> and </em><code>floating-point</code><em> numbers. Special values like </em><code>Infinity</code><em>, </em><code>-Infinity</code><em>, and </em><code>NaN</code><em> represent </em><code>infinite</code><em> values and computational errors, respectively.</em></div>
</div>

```javascript
let n1 = 2;
console.log(n1)

let n2 = 1.3;
console.log(n2)

let n3 = Infinity;
console.log(n3)

let n4 = 'something here too' / 2;
console.log(n4)
```

### 2\. `String`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>A&nbsp;</em><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-strings/" style="pointer-events: none;"><strong><em><u>String</u></em></strong></a><em>&nbsp;in JavaScript is a series of characters that are surrounded by </em><code>quotes</code><em>. There are three types of </em><code>quotes</code><em> in JavaScript, which are </em><code>‘string’</code><em>, </em><code>”string”</code><em>, and </em><code>`string`</code>. <strong><em>(Single quotes, Double quotes, &amp; Backticks)</em></strong>.</div>
</div>

```javascript
let s1 = "Hello There";
console.log(s1); 

let s2 = 'Single quotes work fine';
console.log(s2); 

let s3 = `can embed ${s1}`;
console.log(s3);
```

### 3\. `Boolean`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>The&nbsp;</em><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-boolean/" style="pointer-events: none;"><strong><em><u>boolean</u></em></strong></a><em>&nbsp;type has only </em><code>two</code><em> values i.e. </em><code>true</code><em> and </em><code>false</code><em>.</em></div>
</div>

```javascript
let b1 = true;
console.log(b1);  

let b2 = false;
console.log(b2);
```

### 4\. `null`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>The special&nbsp;</em><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/null-in-javascript/" style="pointer-events: none;"><strong><em><u>null value</u></em></strong></a><em>&nbsp;does not belong to any of the default data types. It forms a separate type of its own which contains only the </em><code>null</code><em> value.</em></div>
</div>

```javascript
const middleName = null;
//Explicitly define empty. null can be said as empty
```

### 5\. `undefined`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>A variable that has been </em><strong><em>declared</em></strong><em> but not </em><strong><em>initialized</em></strong><em> with a value is automatically assigned the&nbsp;</em><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/undefined-vs-null-in-javascript/" style="pointer-events: none;"><strong><em><u>undefined</u></em></strong></a><em>&nbsp;value. It means the variable </em><strong><em>exists</em></strong><em>, but it has no value </em><strong><em>assigned</em></strong><em> to it.</em></div>
</div>

```javascript
const firstName; 
//Value (undefine) taken by a variable that is not yet define.
```

### 6\. `BigInt (Introduced in ES2020)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_self" rel="noopener noreferrer nofollow" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer" href="https://www.geeksforgeeks.org/javascript/javascript-bigint/" style="pointer-events: none;"><strong><em>BigInt</em></strong></a><em>&nbsp;is a </em><code>built-in object</code><em> that provides a way to represent </em><strong><em>whole</em></strong><em> numbers greater than </em><code>253</code><em>. The largest number that JavaScript can reliably represent with the </em><code>Number</code><em> primitive is </em><code>253</code><em>, which is represented by the </em><code>MAX_SAFE_INTEGER</code><em> constant.</em></div>
</div>

```javascript
let b = BigInt("0b1010101001010101001111111111111111");
let largeNumber = 1234576454525657535n;
console.log(b);
console.log(largeNumber);
```

### 7\. `Symbol (Introduced in ES6)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-symbol-method/" style="pointer-events: none;"><strong><em><u>Symbols</u></em></strong></a><em>, introduced in </em><code>ES6</code><em>, are </em><strong><em>unique</em></strong><em> and </em><strong><em>immutable</em></strong><em> primitive values used as </em><code>identifiers</code><em> for </em><code>object</code><em> properties. They help create </em><code>unique keys</code><em> in </em><code>objects</code><em>, preventing conflicts with other properties.</em></div>
</div>

```javascript
let s1 = Symbol("JS");
let s2 = Symbol("JS");
console.log(s1 == s2); //false
```

### 8\. `object (most important)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"></div>
</div>

```javascript
let obj = {
    type: "Company",
    location: "Noida"
}
console.log(obj.type)
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>In JavaScript other than primitive data type is </em></strong><code>object</code><strong><em>. Primitive data types are </em></strong><code>7</code></div>
</div>

### ***Comments***

In programming, we use ***comments*** to literally comment code or ***deactivate*** code without deleting it. We can do comments in two types.

***JS engine*** doesn't parse these comments, they are for ***developers*** and explain the code.

1.  *Single line comment* `(//….)`
    
2.  *Multi line comment* `(/* … */)`
    

```javascript
//let javascript = "FUN!";

/*
if (true) {
    var x = 10;
    let y = 20;
}

console.log(x);  
console.log(y);  
*/
```

### ***Operators in JavaScript***

*JavaScript operators are* `symbols` *or* `keywords` *used to perform* `operations` *on* `values` *and* `variables`*.*

*They are the* `building blocks` *of JavaScript expressions and can manipulate data in various ways.*

***1.Arithmetic Operators*** `(+, -, , /, %, *)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-arithmetic-operators/" style="pointer-events: none;"><strong><em><u>Arithmetic Operators</u></em></strong></a><em>&nbsp;perform mathematical calculations like </em><code>addition</code><em>, </em><code>subtraction</code><em>, </em><code>multiplication</code><em>, etc.</em></div>
</div>

```javascript
const sum = 5 + 3; // Addition
const diff = 10 - 2; // Subtraction
const prodcut = 4 * 2; // Multiplication
const quotient = 8 / 2; // Division
const exponent = 4 ** 2; //Exponentiation
const remainder = 4 % 2; //modulous
console.log(sum, diff, prodcut, quotient, exponent, remainder);
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Important :- </em></strong><code>**</code><strong><em> Exponent operator, it raised the second operand as a power of first operand. </em></strong><code>%</code><strong><em> Modulus operator, it's used to calculate the remainder.</em></strong></div>
</div>

***2.* *Comparison / Relational Operators*** `(==, !=, ===, !==, >, <, >=, <=, in, insteadof)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><em>Used to produce </em><code>boolean</code><em> value based on the comparison result. These are useful for making </em><strong><em>decisions</em></strong><em> in conditional statements. They are used to compare its operands and determine the relationship between them.</em></div>
</div>

```javascript
console.log(10 > 5);
console.log(10 === "10");
```

*   `>` checks if the left value is greater than the right.
    
*   `===` checks for strict equality (both type and value).
    
*   Other operators include `<, <=, >=,` and `!==`.
    

```javascript
const obj = { length: 10 };
console.log("length" in obj);
console.log([] instanceof Array);
```

*   `in` checks if a property exists in an `object`.
    
*   `instanceof` checks if an `object` is an instance of a `constructor`.
    

***3.Assignment Operators*** `(=, +=, -=, *=, /=, %=)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_self" rel="noopener noreferrer" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer notion-link-token notion-focusable-token notion-enable-hover" href="https://www.geeksforgeeks.org/javascript/javascript-assignment-operators/" style="pointer-events: none;"><strong><em><u>Assignment operators</u></em></strong></a><em>&nbsp;are used to assign values to variables. They can also perform operations like </em><strong><em>addition</em></strong><em> or </em><strong><em>multiplication</em></strong><em> while assigning the value. </em><code>+=</code><em> These called </em><strong><em>compound</em></strong><em> assignment operators.</em></div>
</div>

```javascript
let n = 10;
n += 5;
n *= 2;
console.log(n);
```

*   `=` assigns a value to a variable.
    
*   `+=` adds and assigns the result to the variable.
    
*   `=` multiplies and assigns the result to the variable.
    

***4\. Ternary Operator*** `(condition? “true”: “false”)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>It is a shorthand for conditional statements. It takes three operands.</em></strong></div>
</div>

```javascript
const age = 18;
const status = age >= 18 ? "Adult" : "Minor";
console.log(status);
```

> **condition ? expression1 : expression2** evaluates **expression1** if the condition is **true**, otherwise evaluates **expression2**.

*5.* ***Logical Operators*** `(&&, ||, !)`

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>These are mainly used to perform the logical operations that determine the equality or difference between the values.</em></strong></div>
</div>

```javascript
console.log(true && 10);     //output: 10
console.log("Hi" || "Bye"); //output: Hi
console.log(!true);        //output: false
```

> JavaScript doen't always evaluate the entire expression.

As a ***logical expression*** is evaluated ***left to right ,*** JavaScript stops ***e***xecution as soon as the ***final outcome*** is determined\*\*\*.\*\*\* If the result is clear\*\*\*,\*\*\* javascript ***short-circuits (stop)*** the process and ignores the remaining expressions or function calls on the ***right.***

> Logical operators in JavaScript don't just return **true** or **false -** they actually return the **value** of the operand where the evaluation stopped.

*   `falsy && anything` is ***short-circuit*** evaluated to the `falsy` value.
    
*   `truthy || anything` is ***short-circuit*** evaluated to the `truthy` value.
    
*   `nonNullish ?? anything` is ***short-circuit*** evaluated to the `non-nullish` value.
    

***6\. Unary Operators*** `(++, —, +, -, typeof, void, delete)`

> These operators, operate on a single operand.

```javascript
let x = 5;

console.log(+x);
console.log(-x);

console.log(++x);
console.log(--x);
```

*   `+` converts a value to a `number`.
    
*   `-` negates a value (changes its `sign`).
    

++ increments a value by `1`.

\-- decrements a value by `1`.

`typeof` returns the `data type` of a variable.

`delete` removes a property from an `object`.

* * *

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>This is just the beginning. You’ve met JavaScript, now it’s time to see what you can build with it. 🚀</em></strong></div>
</div>
