Introduction to JavaScript
JavaScript is a versatile, dynamically typed programming language that brings life to web pages by making them interactive. It is used for building interactive web applications and supports both client-side and server-side development.
- Interpreted language: Code is executed line by line.
- Dynamically typed: Variable types are determined at runtime.
- Single-threaded: Executes one task at a time (but supports asynchronous operations).
Features of JavaScript
Here are some key features of JavaScript that make it a powerful language for web development:
- Client-Side Scripting: JavaScript runs on the user's browser, so it has a faster response time without needing to communicate with the server.
- Versatile: Can be used for a wide range of tasks, from simple calculations to complex server-side applications.
- Event-Driven: Responds to user actions (clicks, keystrokes) in real-time.
- Asynchronous: It can handle tasks like fetching data from servers without freezing the user interface.
- Rich Ecosystem: There are numerous libraries and frameworks built on JavaScript, such as React, Angular, and Vue.js, which make development faster and more efficient.
Client Side and Server Side nature of JavaScript
JavaScript's flexibility extends to both the client-side and server-side, allowing developers to create complete web applications. Here’s how it functions in each environment:
- Client-Side: Involves controlling the browser and its DOM (Document Object Model). Handles user events like clicks and form inputs. Common libraries include AngularJS, ReactJS, and VueJS.
- Server-Side: Involves interacting with databases, manipulating files, and generating responses. Node.js and frameworks like Express.js are widely used for server-side JavaScript, enabling full-stack development.
What Can JavaScript Do?
- JavaScript is the programming language of the web.
- It can calculate, manipulate and validate data.
- It can update and change both HTML and CSS.
JavaScript Can Change HTML Content
One of many JavaScript HTML methods is getElementById().
What is a JavaScript Compiler?
A compiler is a program that translates code written in one programming language into another language or a lower-level representation. In the context of JavaScript, a compiler translates human-readable JavaScript code into machine code or bytecode that can be executed by the computer’s CPU.
Unlike traditional compiled languages like C++ or Java, JavaScript is typically executed by an interpreter in the browser. However, modern JavaScript engines (like V8, SpiderMonkey, and JavaScriptCore) use a combination of interpretation and compilation to optimize performance. This approach is known as Just-In-Time (JIT) compilation.
JavaScript Data Types
A JavaScript variable can hold 8 types of data.
7 Primitive Data Types and 1 Object Data Type.
JavaScript data types are broadly classified into two categories:
JavaScript Data Types
│
├── Primitive Data Types
│ ├── String
│ ├── Number
│ ├── Boolean
│ ├── Undefined
│ ├── Null
│ ├── BigInt
│ └── Symbol
│
└── Non-Primitive (Reference) Data Types
├── Object
├── Array
└── Function
Key Point:
- Primitive data types store a single value.
- Non-primitive data types store collections of data and are accessed through references.
JavaScript Variables
Variables = Data Containers
JavaScript variables are containers for data.
JavaScript variables can be declared in 4 ways:
Modern JavaScript
- Using let
- Using const
Older JavaScript
- Using var (Not Recommended)
- Automatically (Not Recommended)
Variables in JavaScript are used to store data values. They can be declared in different ways depending on how the value should behave.
- Variables can be declared using var, let, or const.
- JavaScript is dynamically typed, so types are decided at runtime.
- You don’t need to specify a data type when creating a variable.
Declaring Variables in JavaScript
Before ES6 (2015): Variables were declared only with var, which is function-scoped and global-scoped, causing issues like hoisting and global pollution.
ES6 Introduction:let and const were introduced to provide safer alternatives for declaring variables.
Scope: let and const are block-scoped (limited to { } block) or global-scoped, reducing errors compared to var.
1. var keyword
var is a keyword in JavaScript used to declare variables and it is Function-scoped and hoisted, allowing redeclaration but can lead to unexpected bugs.
var a = "Hello Geeks";
var b = 10;
console.log(a);
console.log(b);
2. let keyword
let is a keyword in JavaScript used to declare variables and it is Block-scoped and not hoisted to the top, suitable for mutable variables
let a = 12
let b = "gfg";
console.log(a);
console.log(b);
3. const keyword
const is a keyword in JavaScript used to declare variables and it is Block-scoped, immutable bindings that can't be reassigned, though objects can still be mutated.
const a = 5
let b = "gfg";
console.log(a);
console.log(b);
Rules for Naming Variables
When naming variables in JavaScript, follow these rules
- Variable names must begin with a letter, underscore (_), or dollar sign ($).
- Subsequent 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.
let userName = "Suman"; // Valid let $price = 100; // Valid let _temp = 0; // Valid let 123name = "Ajay"; // Invalid let function = "gfg"; // Invalid
Top comments (2)
Good
Understandable and good 👏