0% found this document useful (0 votes)
2 views

1_introduction_to_javascript_1786795721

This document provides an introduction to JavaScript, covering its definition, execution environment, and characteristics such as being interpreted and single-threaded. It also discusses variables, data types, type conversion, arithmetic operators, and conditional statements like if-else and switch. The document includes examples and assignments to reinforce learning.

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1_introduction_to_javascript_1786795721

This document provides an introduction to JavaScript, covering its definition, execution environment, and characteristics such as being interpreted and single-threaded. It also discusses variables, data types, type conversion, arithmetic operators, and conditional statements like if-else and switch. The document includes examples and assignments to reinforce learning.

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

1 Introduction to Javascript
Table of Content
1. What is Javascript

2. Variables & Data Types

3. Javascript Type Conversion

4. Javascript Arithmetic Operators

5. Conditional statements in Javascript

1. What is Javascript
JavaScript (JS) is a high-level, interpreted, dynamic programming language
primarily used to create interactive and dynamic web content. It is one of the
core technologies of the web, alongside HTML and CSS.

How Does JavaScript Work?


1. Execution Environment: JavaScript runs in the browser's JavaScript
engine (like Google's V8 in Chrome, SpiderMonkey in Firefox, or
JavaScriptCore in Safari). It can also run server-side using environments
like Node.js.

2. Interpreted Nature: JavaScript code is directly executed by the engine, line


by line. However, modern engines use Just-In-Time (JIT) compilation for
better performance.

3. Single-threaded Model: JavaScript runs in a single thread but uses


asynchronous mechanisms (like callbacks, promises, and async/await) to
handle tasks like API calls or timers.

Compiler or Interpreter?
JavaScript is both interpreted and compiled, depending on the context:

Traditionally interpreted: JavaScript engines are used to interpret the code


line by line directly.

1.1 Introduction to Javascript 1


Modern engines use JIT compilation: Engines like V8, Chakra, and
SpiderMonkey compile JavaScript to optimize machine code at runtime
(Just-In-Time) for faster execution.

Learn more: https://dev.to/suprabhasupi/how-javascript-works-4ked

Where to put the Javascript code?


In HTML, JavaScript code is inserted between <script> and </script> tags.

<script>
document.getElementById("demo").innerHTML = "My First JavaS
</script>

You can place any number of scripts in an HTML document.


Scripts can be placed in the <body> , or in the <head> section of an HTML
page, or in both.

We can also create separate External Javascript.

<script src="myScript1.js"></script>

Keywords

1.1 Introduction to Javascript 2


In JavaScript, you cannot use these reserved words as variables, labels, or
function names:

Javascript Statements

A computer program is a list of "instructions" to be "executed" by a


computer.
In a programming language, these programming instructions are
called statements.

A JavaScript program is a list of programming statements.

let a, b, c; // Declare 3 variables


a = 5; // Assign the value 5 to a
b = 6; // Assign the value 6 to b
c = a + b; // Assign the sum of a and b to c

2. Variables & Data Types


Variables

1.1 Introduction to Javascript 3


Variables are containers for storing data (storing data values). We can
declare a variable by using these keywords.

Using var for declaring function-scoped variables (old)

Using let for declaring block-scoped variables (new)

Using const for declaring constant variables

Note: It is recommended we use let instead of var. However, there are a few
browsers that do not support let.

Rules for naming variables:

1. Variable names must start with either a letter, an underscore _ , or the


dollar sign $ . For example,

//valid
let a = 'hello';
let _a = 'hello';
let $a = 'hello';

2. Variable names cannot start with numbers. For example,

//invalid
Let 1a = 'hello'; // this gives an error

3. JavaScript is case-sensitive. For example,

let y = "hi";
let Y = 5;

console.log(y); // hi
console.log(Y); // 5

4. Keywords cannot be used as variable names. For example,

//invalid
let new = 5; // Error! new is a keyword.

1.1 Introduction to Javascript 4


Data Types
A data type, in programming, is a classification that specifies which type of
value a variable has and what type of mathematical, relational or logical
operations can be applied to it without causing an error.
A string, for example, is a data type that is used to classify text and an
integer is a data type used to classify whole numbers.

Some Common Data Types in Javascript are:

String for “Hello”, ‘hi’ etc.

Number for 45, -12 etc.

Boolean for true and false

undefined for un-initialized variables

Object key-value pairs of collection of data

3. Javascript Type Conversion


We use these functions to convert types:

Number()

String()

Boolean()

Note:

1.1 Introduction to Javascript 5


1. JavaScript considers 0 as false and all non-zero numbers as true. And, if
true is converted to a number, the result is always 1.

2. String() takes null and undefined and converts them to string.

3. In JavaScript, undefined , null , 0 , NaN , '' converts to false. All other values
give true .

Use this table for reference:

4. Javascript Arithmetic Operators


As with algebra, you can do arithmetic with JavaScript variables, using
operators like = and +

const number = 3 + 5; // 8

We have Arithmetic Operators : +, -, /, *, ++, — and **

1.1 Introduction to Javascript 6


5. Javascript Comparison Operators

ternary Operator

1.1 Introduction to Javascript 7


A ternary operator evaluates a condition and executes a block of code
based on the condition.

Its syntax is:

condition ? expression1 : expression2


let result = (marks >= 40) ? 'pass' : 'fail';

The ternary operator evaluates the test condition.

If the condition is true , expression1 is executed.

If the condition is false , expression2 is executed.

The ternary operator takes three operands, hence, the name ternary
operator. It is also known as a conditional operator.

If-else, else-if
In computer programming, there may arise situations where you have to run
a block of code among more than one alternatives. For example, assigning
grades A, B or C based on marks obtained by a student.
In such situations, you can use the JavaScript if...else statement to create
a program that can make decisions.

if (condition) {
// block of code if condition is true
} else {
// block of code if condition is false
}

You can also write multiple else if in between the if and the else blocks.

logical Operators

1.1 Introduction to Javascript 8


Switch Statements

The JavaScript switch statement is used in decision making.


The switch statement evaluates an expression and executes the
corresponding body that matches the expression's result.

// program using switch statement


let a = 2;

switch (a) {
case 1:
a = 'one';
break;
case 2:
a = 'two';
break;
default:
a = 'not found';
break;
}
console.log(`The value is ${a}`);

1.1 Introduction to Javascript 9


Assignments
1. Build a Calculator Application (without the UI) using Arithmetic operators

2. Build an Average Marks Generator. using Arithmetic operators

3. Build a BMI calculator using Arithmetic operators

4. Build a program to grade students based on marks using switch-case

1.1 Introduction to Javascript 10

You might also like