0% found this document useful (0 votes)
7K views12 pages

PRIMITIVE HTML

The document discusses JavaScript primitive types, operators, expressions, and input/output. It covers the main primitive types in JavaScript as well as various operators for arithmetic, comparison, logical, assignment, bitwise and string operations. It also discusses expressions, conditional expressions, and assignment operators.

Uploaded by

Sukanya Nagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views12 pages

PRIMITIVE HTML

The document discusses JavaScript primitive types, operators, expressions, and input/output. It covers the main primitive types in JavaScript as well as various operators for arithmetic, comparison, logical, assignment, bitwise and string operations. It also discusses expressions, conditional expressions, and assignment operators.

Uploaded by

Sukanya Nagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PRIMITIVE TYPES

▪ JavaScript has five primitive types: Number, String, Boolean, Undefined, and Null.
▪ Each primitive value has one of these types.
▪ JavaScript includes predefined objects that are closely related to the Number, String, and
Boolean types, named Number, String, and Boolean, respectively.
▪ These objects are called wrapper objects.
▪ Each contains a property that stores a value of the corresponding primitive type.
▪ The purpose of the wrapper objects is to provide properties and methods that are convenient
for use with values of the primitive types.
▪ The difference between primitives and objects is shown in the following example.

Primitive data types: The predefined data types provided by JavaScript


language are known as primitive data types. Primitive data types are also
known as in-built data types.
Below is a list of Primitive Data Types with proper descriptions and
examples:
1. Number: Number data type in javascript can be used to hold decimal values
as well as values without decimals.
 let x = 250;
    let y = 40.5;
 console.log("Value of x=" + x);
    console.log("Value of y=" + y);
2. String: The string data type in javascript represents a sequence of
characters that are surrounded by single or double quotes.

In any computer programming language, a string is a sequence of characters used to


represent text.

In JavaScript, a String is one of the primitive values and the String object is


a wrapper around a String primitive.

 Javascript

    let str = 'Hello All';

    let str1 = "Welcome to my new house";

    console.log("Value of str=" + str);


    console.log("Value of str1=" + str1);

3. Undefined: The meaning of undefined is ‘value is not assigned’.

undefined is a primitive value automatically assigned to variables that have just been declared, or


to formal arguments for which there are no actual arguments.

 console.log("Value of x=" + x);

4. Boolean: The boolean data type can accept only two values i.e. true and
false.
console.log("value of bool=" + bool);

5. Null: This data type can hold only one possible value that is null. The null in
JavaScript is a data type that is represented by only one value, the ‘null’ itself. A null value
means no value.

let x = null;
    console.log("Value of x=" + x);
Operators
JavaScript has arithmetic, string, and logical operators. There are
both binary and unary operators. A binary operator requires two operands, one before
the operator and one after the operator:
operand1 operator operand2

For example, 3 + 4 or x * y

A unary operator requires a single operand, either before or after the operator:
operator operand

or
operand operator

For example x++ or ++x.

JavaScript Arithmetic Operators


Arithmetic operators are used to perform arithmetic between variables and/or
values.

Oper Name Example Results

+ Addition x=y+2 y=5, x=7


- Subtraction x=y-2 y=5, x=3

* Multiplication x=y*2 y=5, x=10

** Exponentiation x=y**2 y=5, x=25


ES2016

/ Division x=y/2 y=5, x=2.5

% Remainder x=y%2 y=5, x=1

++ Pre increment x = ++y y=6, x=6

++ Post increment x = y++ y=6, x=5

-- Pre decrement x = --y y=4, x=4

-- Post decrement x = y-- y=4, x=5

JavaScript Assignment Operators


Assignment operators are used to assign values to JavaScript variables.

Given that x = 10 and y = 5, the table below explains the assignment


operators:

Oper Example Same As Result Try

= x=y x=y x=5

+= x += y x=x+y x = 15 Try

-= x -= y x=x-y x=5 Try

*= x *= y x=x*y x = 50 Try

/= x /= y x=x/y x=2 Try

%= x %= y x=x%y x=0 Try

: x: 45 size.x = 45 x = 45

Comparison Operators
Comparison operators are used in logical statements to determine equality or
difference between variables or values.

Given that x = 5, the table below explains the comparison operators:


Oper Name Comparing Returns

== equal to x == 8 false

== equal to x == 5 true

=== equal value and type x === "5" false

=== equal value and type x === 5 true

!= not equal x != 8 true

!== not equal value or type x !== "5" true

!== not equal value or type x !== 5 false

> greater than x>8 false

< less than x<8 true

>= greater or equal to x >= 8 false


<= less or equal to x <= 8 true

Logical Operators
Logical operators are used to determine the logic between variables or values.

Given that x = 6 and y = 3, the table below explains the logical operators:

Oper Name Example

&& AND (x < 10 && y > 1) is true

|| OR (x === 5 || y === 5) is false

! NOT !(x === y) is true

JavaScript Bitwise Operators


Bit operators work on 32 bits numbers. Any numeric operand in the operation is
converted into a 32 bit number. The result is converted back to a JavaScript
number.

Oper Name Example Same as Result Decimal

& AND x=5&1 0101 & 0001 0001 1


| OR x=5|1 0101 | 0001 0101 5

~ NOT x=~5 ~0101 1010 10

^ XOR x=5^1 0101 ^ 0001 0100 4

<< Left shift x = 5 << 1 0101 << 1 1010 10

>> Right shift x = 5 >> 1 0101 >> 1 0010 2

>>> Unsigned right x = 5 >>> 1 0101 >>> 1 0010 2

JavaScript String Operators


The + operator, and the += operator can also be used to concatenate (add) strings.

Given that t1 = "Good ", t2 = "Morning", and t3 = "", the table below explains the operators:

Oper Example t1 t2 t3

+ t3 = t1 + t2 "Good " "Morning"  "Good Morning"


+= t1 += t2 "Good Morning" "Morning"

Expressions
An expression is any valid set of literals, variables, operators, and expressions that
evaluates to a single value. The value may be a number, a string, or a logical value.
Conceptually, there are two types of expressions: those that assign a value to a
variable, and those that simply have a value. For example, the expression
x = 7

is an expression that assigns x the value 7. This expression itself evaluates to 7. Such
expressions use assignment operators. On the other hand, the expression
3 + 4

simply evaluates to 7; it does not perform an assignment. The operators used in such
expressions are referred to simply as operators.

JavaScript has the following kinds of expressions:

 Arithmetic: evaluates to a number, for example


 String: evaluates to a character string, for example "Fred" or "234"
 Logical: evaluates to true or false

The special keyword null denotes a null value. In contrast, variables that have not
been assigned a value are undefined, and cannot be used without a run-time error.

Conditional Expressions

A conditional expression can have one of two values based on a condition. The syntax
is
(condition) ? val1 : val2
If condition is true, the expression has the value of val1, Otherwise it has the value
of val2. You can use a conditional expression anywhere you would use a standard
expression.

For example,
status = (age >= 18) ? "adult" : "minor"
This statement assigns the value "adult" to the variable status if age is eighteen or
greater. Otherwise, it assigns the value "minor" to status.

Assignment Operators (=, +=, -=, *=, /=)


An assignment operator assigns a value to its left operand based on the value of its
right operand. The basic assignment operator is equal (=), which assigns the value of
its right operand to its left operand. That is, x = y assigns the value of y to x.

The other operators are shorthand for standard arithmetic operations as follows:

 x += y means x = x + y
 x -= y means x = x - y
 x *= y means x = x * y
 x /= y means x = x / y
 x %= y means x = x % y

There are additional assignment operators for bitwise operations:

 x <<= y means x = x << y


 x >>= y means x = x >> y
 x >>>= means x = x >>> y
 x &= means x = x & y
 x ^= means x = x ^ y
 x |= means x = x | y

Screen output and keyboard input.

The JavaScript model for the HTML document is


the Document object
 The model for the browser display window is
the Window object
o The Window object has two

properties, document and window, which refer to


the document and window objects, respectively
 The Document object has a method, write, which
dynamically creates content
o The parameter is a string, often catenated from

parts, some of which are variables


e.g., document.write("Answer: " + result + "<br />");
 The Window object has three methods for creating
dialog boxes, alert, confirm, and prompt
The default object for JavaScript is the Window
object currently being displayed, so calls to these
methods need not include an object reference.
1. alert("Hej! \n");

 Parameter is plain text, not HTML


 Opens a dialog box which displays the
parameter string and an OK button
2. confirm("Do you want to continue?");

 Opens a dialog box and displays the


parameter and two buttons, OK and Cancel
3. prompt("What is your name?", "");

 Opens a dialog box and displays its string


parameter, along with a text box and two
buttons, OK and Cancel
 The second parameter is for a default
response if the user presses OK without
typing a response in the text box (waits
for OK)
Find solutions for a quadratic equation
<!DOCTYPE html>
<!-- roots.html
A document for roots.js
-->
<html lang = "en">
<head>
<title> roots.html </title>
<meta charset = "utf-8" />
</head>
<body>
<script type = "text/javascript" src = "roots.js" >
</script>
</body>
</html>

You might also like