All you need to know about JavaScript!
Sixhei

Sixhei

August 14th 2022

All you need to know about JavaScript!

-What is JavaScript?

JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved.
It is the third layer of the layer cake of standard web technologies, two of which (HTML and CSS) we have covered in much more detail in other parts of the Learning Area.

- We will start with "typeof".

The typeof operator returns a string indicating the type of the unevaluated operand.

Syntax:

typeof operand

Description:

The following table summarizes the possible return values of typeof.
Type
Result
Undefined
"undefined"
Null
"object"
Boolean
"boolean"
Number
"number"
BigInt
"bigint"
String
"string"
Symbol
"symbol"
Function
"function"
Any other object
"object"

Examples:

Basic Usage
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof 42 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number';
 
// Despite being "Not-A-Number"
typeof Number('1') === 'number'; 
     
// Number tries to parse things into numbers
typeof Number('shoe') === 'number';  
 
// including values that cannot be type coerced to a number
typeof 42n === 'bigint';

// Strings
typeof '' === 'string';
typeof 'bla' === 'string';
typeof `template literal` === 'string';
typeof '1' === 'string'; 

// note that a number within a string is still typeof string
typeof (typeof 1) === 'string'; 

// typeof always returns a string
typeof String(1) === 'string'; 
// String converts anything into a string, safer than toString

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; 

// Boolean() will convert values based on if they're truthy or falsy
typeof !!(1) === 'boolean'; 
// two calls of the ! (logical NOT) operator are equivalent to Boolean()

// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'

// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';

// Objects
typeof { a: 1 } === 'object';
// use Array.isArray or Object.prototype.toString.call

// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
typeof /regex/ === 'object'; 
// See Regular expressions section for historical results

// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';

// Functions
typeof function () {} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';

typeof null

typeof null === 'null'

- We will continue with "functions()".

Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output. To use a function, you must define it somewhere in the scope from which you wish to call it.

Defining functions:

A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:
  • The name of the function.
  • A list of parameters to the function, enclosed in parentheses and separated by commas.
  • The JavaScript statements that define the function, enclosed in curly brackets, { /* … */ }.
For example, the following code defines a simple function named square:
function square(number) {
return number * number;
}
The function square takes one parameter, called number. The function consists of one statement that says to return the parameter of the function (that is, number) multiplied by itself. The statement return specifies the value returned by the function:
return number * number;
Parameters are essentially passed to functions by value — so if the code within the body of a function assigns a completely new value to a parameter that was passed to the function, the change is not reflected globally or in the code which called that function.
When you pass an object as a parameter, if the function changes the object's properties, that change is visible outside the function, as shown in the following example:
function myFunc(theObject) {
  theObject.make = 'Toyota';
}

const mycar = {
make: 'Honda',
model: 'Accord',
year: 1998,
};

// x gets the value "Honda"
const x = mycar.make;

// the make property is changed by the function
myFunc(mycar);
// y gets the value "Toyota"
const y = mycar.make;
When you pass an array as a parameter, if the function changes any of
the array's values, that change is visible outside the function, as
shown in the following example:
function myFunc(theArr) {
  theArr[0] = 30;
}

const arr = [45];

console.log(arr[0]); // 45
myFunc(arr);
console.log(arr[0]); // 30

- Bonus "toLocaleString()".

The toLocaleString() method returns a string with a language-sensitive representation of this date.

Try it:

Return value:

A string representing the given date according to language-specific conventions.

Sixhei

A university student who helped create the forum site.

Leave a Reply

Postimet e Ngjashme

Kategoritë

Pyetësor

JAVA

C++

Inxhinieria Software

Baze te dhenash

Javascript

Multimedia

Termodinamikë

© 2022 Univlora Student Forum