Posted on Leave a comment

Face Recognition (from picture ) code in JavaScript

Unfortunately, JavaScript alone cannot perform face recognition directly on images as it lacks the necessary computer vision libraries. You can use JavaScript in combination with a server-side API that uses computer vision libraries to perform face recognition on images. Here’s a code example that uses the Clarifai API to perform face recognition:

const Clarifai = require('clarifai');

const app = new Clarifai.App({
 apiKey: 'your_api_key'
});

const imageURL = 'https://s3.amazonaws.com/clarifai-api/img/face-det.jpg';

app.models.predict(Clarifai.FACE_DETECT_MODEL, imageURL)
 .then(response => {
   console.log(response.outputs[0].data);
 })
 .catch(err => {
   console.error(err);
 });

This code uses the clarifai npm package to interact with the Clarifai API. You’ll need to sign up for an API key and replace 'your_api_key' with your own API key. Once you run this code, it will print out information about any faces detected in the image located at imageURL.

Posted on Leave a comment

Why are Actions Not Allowed in Strict Mode?

Some typical actions that are forbidden in strict mode are given below:

1. Undeclared objects or variables.

We have already seen that using a variable or an object without declaring it is not allowed in strict mode.

Example:

"use strict";
user = {
    name: 'John',
    age: 30,
    gender: 'male',
}
 
console.log(user.name) // John

Output:

user = {
referenceError: User is not defined

2. Deleting an object, variable, or function.

Example:

"use strict";
let user = {
    name: 'John',
    age: 30,
    gender: 'male',
}
 
delete user; // Error here

Output:

delete user = {
SyntaxError: Delete of …..

3. Duplicating a parameter name.

Example:

"use strict";
functionhello(value, value){    
console.log(value + " "+ value);}

Output:

function hello(value, name){

user.name = “John”;

4. Writing to a read-only property, get-only property & undeletable property is not allowed.

Example:

"use strict";
const user = {};
Object.defineProperty(user, "name", {value:0, writable:false});
 
user.name = "John";

Output:

user.name = “John”;

TypeError: Cannot …

4. Octal numeric literals & Octal escape characters are not allowed.

Example:

"use strict";
letnum = "\010";  
// Error here

Output:

let num = "\010";   // Error here

SyntaxError:…

5. ‘eval’ cannot be used as a variable.

Example:

"use strict";
let eval = "Hello World";
 
console.log(eval);

Output:

let eval = "Hello World";

SyntaxError:…

6. ‘arguments’ cannot be used as a variable.

Example:

"use strict";
let arguments = "Hello World";
 
console.log(arguments);

Output:

let arguments = “Hello World”;

SyntaxError:…

7. ‘with’ is not allowed.

Example:

"use strict";
with ([1, 2, 3]) {
    console.log(toString()); 
}

Output:

with ([1, 2, 3]) {

SyntaxError:…

8. Keywords such as implements, interface, static, package, private, protected, public, let and yield are prohibited in strict mode.

Example:

"use strict";
let public = "public"; 

Output:

let public = “public”;

SyntaxError:…

Posted on Leave a comment

Activating JavaScript Strict Mode in Individual Functions

You can also enable string mode for a particular function by adding  ‘use strict’; or “use strict”; at the beginning of the function.

Syntax:

functionfunctionName() {    ‘use strict’;    // function body}// orfunctionfunctionName() {    "use strict";    // function body}

This statement should be the first statement of the function excluding comments.

Adding ‘use strict’; or “use strict”; at the beginning of a function has a local scope which means all the code in that script is executed usually and the code inside the function will only execute in strict mode.

Example:

functionName() {  // Function without strict mode    name = "Rack"    console.log("Name is: "+ name) // No error here}functionAge() {  // Function with strict mode    "use strict";    age = "22"    console.log("Age is: "+ age) // Error here}Name()Age()

Output:

Name is: RackC:\Users\ag290\OneDrive\Desktop\journalDev-2023\january\strict mode\app.js:8    age = "22"        ^ReferenceError: age is not defined
Posted on Leave a comment

JavaScript Strict Mode: What it Does and Doesn’t Allow

In this tutorial, you will learn about the strict mode in JavaSript, the process of activating strict mode for a program or an individual function in JavaScript, and some typical actions that are forbidden by strict mode in JavaScript.

Activating JavaScript Strict mode

For activating Strict mode, you can add ‘use strict’; or “use strict”; at the beginning of the script.

Syntax:

‘use strict’;// or"use strict";

This statement should be the first statement in the script excluding comments as JavaScript just ignore them.

If there is any other statement before ‘use strict’; or “use strict”; then string mode will not activate.

Adding ‘use strict’; or “use strict”; at the beginning of the script has a global scope which makes all the code in that script executed in string mode.

Example:

"use strict";name = "John";console.log(name);

Output:

name = "John";

ReferenceError: name is not defined

Without Strict Mode:

name = "John";console.log(name);

Output:

John
Posted on Leave a comment

Q. Explain what a callback function is and provide a simple example

callback the function is a function that is passed to another function as an argument and is executed after some operation has been completed. Below is an example of a simple callback function that logs to the console after some operations have been completed.

function modifyArray(arr, callback) {
  // do something to arr here
  arr.push(100);
  // then execute the callback function that was passed
  callback();
}

var arr = [1, 2, 3, 4, 5];

modifyArray(arr, function() {
  console.log("array has been modified", arr);
});
Posted on Leave a comment

Q. Explain is Scope in JavaScript?

In JavaScript, each function gets its own scope. The scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function’s scoped variables.

A variable name has to be unique within the same scope. A scope can be nested inside another scope. If one scope is nested inside another, code inside the innermost scope can access variables from either scope.

Posted on Leave a comment

Q. What is the object type?

The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type.

var obj = {
	a: "hello world", // property
	b: 42,
	c: true
};

obj.a;		// "hello world", accessed with doted notation
obj.b;		// 42
obj.c;		// true

obj["a"];	// "hello world", accessed with bracket notation
obj["b"];	// 42
obj["c"];	// true

Bracket notation is also useful if you want to access a property/key but the name is stored in another variable, such as:

var obj = {
	a: "hello world",
	b: 42
};

var b = "a";

obj[b];			// "hello world"
obj["b"];		// 42
Posted on Leave a comment

Q. What is equality in JavaScript ?

JavaScript has both strict and type–converting comparisons:

  • Strict comparison (e.g., ===) checks for value equality without allowing coercion
  • Abstract comparison (e.g. ==) checks for value equality with coercion allowed
var a = "42";
var b = 42;

a == b;			// true
a === b;		// false

Some simple equality rules:

  • If either value (aka side) in comparison could be the true or false value, avoid == and use ===.
  • If either value in comparison could be of these specific values (0"", or [] — empty array), avoid == and use ===.
  • In all other cases, you’re safe to use ==. Not only is it safe, but in many cases, it simplifies your code in a way that improves readability.
Posted on Leave a comment

Q. What is typeof operator?

JavaScript provides a typeof operator that can examine a value and tell you what type it is:

var a;
typeof a;				// "undefined"

a = "hello world";
typeof a;				// "string"

a = 42;
typeof a;				// "number"

a = true;
typeof a;				// "boolean"

a = null;
typeof a;				// "object" -- weird, bug

a = undefined;
typeof a;				// "undefined"

a = { b: "c" };
typeof a;				// "object"